Merge pull request #189 from EdwinLove/update_unique_object_docs

Update UniqueObject Validator docs
This commit is contained in:
William Durand 2012-11-05 08:17:11 -08:00
commit 4b707fe42a

View file

@ -1,8 +1,14 @@
The 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:
In a form, if you want to validate the uniqueness of a field in a table you have to use the `UniqueObjectValidator`.
You may use as many validators of this type as you want.
YAML
----
You can specify this using the `validation.yml` file, like this:
``` yaml
Acme\DemoBundle\Model\User:
@ -10,7 +16,7 @@ Acme\DemoBundle\Model\User:
- Propel\PropelBundle\Validator\Constraints\UniqueObject: username
```
For validate the unicity of more than just one fields:
If you want to validate the uniqueness of more than just one field:
``` yaml
Acme\DemoBundle\Model\User:
@ -18,7 +24,56 @@ Acme\DemoBundle\Model\User:
- Propel\PropelBundle\Validator\Constraints\UniqueObject: [username, login]
```
As many validator of this type as you want can be used.
PHP
---
You can also specify this using php. Fields can be specified as a string if there is only one field
``` php
use Propel\PropelBundle\Validator\Constraint\UniqueObject;
...
/**
* Load the Validation Constraints
*
* @param ClassMetadata $metadata
*/
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addConstraint(
new UniqueObject(
array(
'fields' => 'username',
'message' => 'We already have a user with {{ fields }}'
)
)
);
}
```
If there is more than one field you must use an array
``` php
...
$metadata->addConstraint(
new UniqueObject(
array(
'fields' => array('username', 'login')
'message' => 'We already have a user with {{ fields }}'
)
)
);
...
```
[Back to index](index.markdown)