Permalink
Browse files

add a new errorPath parameter, fix outdated doc

  • Loading branch information...
1 parent fa0a8f7 commit c908de92fba5c27dc953441ef29da75d7e8f27aa @jaugustin jaugustin committed Jan 4, 2013
@@ -5,6 +5,17 @@ In a form, if you want to validate the uniqueness of a field in a table you have
You may use as many validators of this type as you want.
+The validator has 1 required parameter:
+
+* `fields` : a field or an array of fields to test for uniqueness
+
+and 3 optionals parameters:
+
+* `message` : the error message with two variable `{{ object_class }}` and `{{ fields }}`
+* `messageFieldSeparator` : the field separator ` and `
+* `errorPath` : the relative path where the error will be attached, if none is set the error is global.
+
+
YAML
----
@@ -13,15 +24,29 @@ You can specify this using the `validation.yml` file, like this:
``` yaml
Acme\DemoBundle\Model\User:
constraints:
- - Propel\PropelBundle\Validator\Constraints\UniqueObject: username
+ - Propel\PropelBundle\Validator\Constraints\UniqueObject:
+ fields: username
```
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]
+ - Propel\PropelBundle\Validator\Constraints\UniqueObject:
+ fields: [username, login]
+```
+
+Full configuration :
+
+``` yaml
+Acme\DemoBundle\Model\User:
+ constraints:
+ - Propel\PropelBundle\Validator\Constraints\UniqueObject:
+ fields: [username, login]
+ message: We already have a user with {{ fields }}
+ messageFieldSeparator: " and "
+ errorPath: username
```
PHP
@@ -45,7 +70,9 @@ use Propel\PropelBundle\Validator\Constraint\UniqueObject;
new UniqueObject(
array(
'fields' => 'username',
- 'message' => 'We already have a user with {{ fields }}'
+ 'message' => 'We already have a user with {{ fields }}',
+ 'messageFieldSeparator' => ' and '
+ 'errorPath' => 'username',
)
)
);
@@ -61,8 +88,10 @@ If there is more than one field you must use an array
$metadata->addConstraint(
new UniqueObject(
array(
- 'fields' => array('username', 'login')
- 'message' => 'We already have a user with {{ fields }}'
+ 'fields' => array('username', 'login'),
+ 'message' => 'We already have a user with {{ fields }}',
+ 'messageFieldSeparator' => ' and ',
+ 'errorPath' => 'username'
)
)
);
@@ -72,8 +101,24 @@ If there is more than one field you must use an array
```
+XML
+---
+
+You can also specify this using xml
+```xml
+ <class name="Acme\DemoBundle\Model\User">
+
+ <constraint name="Propel\PropelBundle\Validator\Constraints\UniqueObject">
+ <option name="fields">username</option>
+ <option name="message">We already have a user with {{ fields }}</option>
+ <option name="messageFieldSeparator"> and </option>
+ <option name="errorPath">username</option>
+ </constraint>
+
+ </class>
+```
[Back to index](index.markdown)
@@ -37,6 +37,11 @@ class UniqueObject extends Constraint
*/
public $fields = array();
+ /**
+ * @var string Used to set the path where the error will be attached, default is global.
+ */
+ public $errorPath;
+
public function __construct($options = null)
{
parent::__construct($options);
@@ -48,6 +53,10 @@ public function __construct($options = null)
if (0 === count($this->fields)) {
throw new ConstraintDefinitionException("At least one field must be specified.");
}
+
+ if (null !== $this->errorPath && !is_string($this->errorPath)) {
+ throw new UnexpectedTypeException($this->errorPath, 'string or null');
+ }
}
/**
@@ -61,13 +61,15 @@ public function validate($object, Constraint $constraint)
);
}
- $this->context->addViolation(
+ $this->context->addViolationAtSubPath(
+ $constraint->errorPath,
$constraint->message,
array(
'{{ object_class }}' => $class,
'{{ fields }}' => implode($constraint->messageFieldSeparator, $fieldParts)
)
);
+
}
}
}

0 comments on commit c908de9

Please sign in to comment.