forked from propelorm/PropelBundle
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,79 @@ | ||
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: | ||
constraints: | ||
- 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: | ||
constraints: | ||
- 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) |