Coding standards modification and update of the doc to explain the use of the uniqueObjectValidator

This commit is contained in:
Maxime AILLOUD 2011-11-04 18:04:53 +01:00
parent 06ef181823
commit 0139b34f57
2 changed files with 25 additions and 5 deletions

View file

@ -352,4 +352,24 @@ public function myAction(Post $post)
}
```
## UniqueObjectValidator ##
In a form, if you want to validate the unicity of a field in a table you have to use the uniqueObjectValidator.
The only way to use it is in a validation.yml file, like this:
``` yaml
BundleNamespace\Model\User:
constraints:
- Propel\PropelBundle\Validator\Constraints\UniqueObject: username
```
For validate the unicity of more than just one fields:
``` yaml
BundleNamespace\Model\User:
constraints:
- Propel\PropelBundle\Validator\Constraints\UniqueObject: [username, login]
```
As many validator of this type as you want can be used.

View file

@ -11,9 +11,9 @@
namespace Propel\PropelBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Unique Object Validator checks if one or a set of fields contain unique values.
@ -35,7 +35,7 @@ class UniqueObjectValidator extends ConstraintValidator
$fields = (array)$constraint->fields;
if (count($fields) == 0) {
if (0 === count($fields)) {
throw new ConstraintDefinitionException("At least one field must be specified.");
}
@ -63,15 +63,15 @@ class UniqueObjectValidator extends ConstraintValidator
$constraintMessage .= ' with';
foreach ($fields as $fieldName) {
$constraintMessage .= ' ' . $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME) . ' "' . $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME) . '" and';
$constraintMessage .= sprintf(' %s "%s" and', $peerClass::translateFieldName($fieldName, \BasePeer::TYPE_FIELDNAME, \BasePeer::TYPE_PHPNAME), $object->getByName($fieldName, \BasePeer::TYPE_FIELDNAME));
}
$constraintMessage = substr($constraintMessage, 0, -4) . '.';
$this->setMessage($constraintMessage);
return false;
}
return true;
}
}