Browse files

Update UniqueObject Validator docs

Add php usage and correct some English
  • Loading branch information...
1 parent cca4fd7 commit 6f21cb07efa9eb4acf3b0124897e86ffa6ad6ce8 EdwinLove committed with Edwin Nov 5, 2012
Showing with 59 additions and 4 deletions.
  1. +59 −4 Resources/doc/unique_object_validator.markdown
View
63 Resources/doc/unique_object_validator.markdown
@@ -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)

0 comments on commit 6f21cb0

Please sign in to comment.