Skip to content

Commit

Permalink
add a new errorPath parameter, fix outdated doc
Browse files Browse the repository at this point in the history
  • Loading branch information
jaugustin committed Jan 4, 2013
1 parent fa0a8f7 commit c908de9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
55 changes: 50 additions & 5 deletions Resources/doc/unique_object_validator.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
----

Expand All @@ -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
Expand All @@ -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',
)
)
);
Expand All @@ -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'
)
)
);
Expand All @@ -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)
9 changes: 9 additions & 0 deletions Validator/Constraints/UniqueObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Validator/Constraints/UniqueObjectValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.