Skip to content

Commit

Permalink
Update UniqueObject Validator docs
Browse files Browse the repository at this point in the history
Add php usage and correct some English
  • Loading branch information
EdwinLove committed Nov 5, 2012
1 parent cca4fd7 commit 6f21cb0
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions Resources/doc/unique_object_validator.markdown
@@ -1,24 +1,79 @@
The UniqueObjectValidator The UniqueObjectValidator
========================= =========================


In a form, if you want to validate the unicity of a field in a table you have to use the `UniqueObjectValidator`. In a form, if you want to validate the uniqueness 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:
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 ``` yaml
Acme\DemoBundle\Model\User: Acme\DemoBundle\Model\User:
constraints: constraints:
- Propel\PropelBundle\Validator\Constraints\UniqueObject: username - 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 ``` yaml
Acme\DemoBundle\Model\User: Acme\DemoBundle\Model\User:
constraints: constraints:
- Propel\PropelBundle\Validator\Constraints\UniqueObject: [username, login] - 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) [Back to index](index.markdown)

0 comments on commit 6f21cb0

Please sign in to comment.