[Form] Fixed arrays not to be passed to the validator

This commit is contained in:
Bernhard Schussek 2011-02-02 17:32:24 +01:00
parent 1c3e3f7744
commit 7c9c7af863
2 changed files with 34 additions and 13 deletions

View File

@ -736,16 +736,19 @@ class Form extends Field implements \IteratorAggregate, FormInterface
throw new MissingOptionsException('The option "validator" is required for validating', array('validator')); throw new MissingOptionsException('The option "validator" is required for validating', array('validator'));
} }
// Validate the submitted data in the domain object in the sets // Only validate data if it is a domain object
// validation group(s) if (is_object($this->getData())) {
if ($violations = $validator->validate($this->getData(), $groups)) { // Validate the submitted data in the domain object in the sets
foreach ($violations as $violation) { // validation group(s)
$propertyPath = new PropertyPath($violation->getPropertyPath()); if ($violations = $validator->validate($this->getData(), $groups)) {
$iterator = $propertyPath->getIterator(); foreach ($violations as $violation) {
$iterator->next(); // point at the first data element $propertyPath = new PropertyPath($violation->getPropertyPath());
$error = new DataError($violation->getMessageTemplate(), $violation->getMessageParameters()); $iterator = $propertyPath->getIterator();
$iterator->next(); // point at the first data element
$error = new DataError($violation->getMessageTemplate(), $violation->getMessageParameters());
$this->addError($error, $iterator); $this->addError($error, $iterator);
}
} }
} }

View File

@ -200,13 +200,13 @@ class FormTest extends \PHPUnit_Framework_TestCase
public function testBindUsesValidationGroups() public function testBindUsesValidationGroups()
{ {
$field = $this->createMockField('firstName');
$form = new Form('author', array( $form = new Form('author', array(
'validation_groups' => 'group', 'validation_groups' => 'group',
'validator' => $this->validator, 'validator' => $this->validator,
)); ));
$form->add($field); $form->add(new TestField('firstName'));
// both the form and the object are validated
$this->validator->expects($this->exactly(2)) $this->validator->expects($this->exactly(2))
->method('validate'); ->method('validate');
@ -221,8 +221,26 @@ class FormTest extends \PHPUnit_Framework_TestCase
// ->method('validate') // ->method('validate')
// ->with($this->equalTo($form)); // ->with($this->equalTo($form));
// data is irrelevant // concrete request is irrelevant
$form->bind($this->createPostRequest()); // data is an object
$form->bind($this->createPostRequest(), new Author());
}
public function testBindDoesNotValidateArrays()
{
$form = new Form('author', array(
'validator' => $this->validator,
));
$form->add(new TestField('firstName'));
// only the form is validated
$this->validator->expects($this->once())
->method('validate')
->with($this->equalTo($form));
// concrete request is irrelevant
// data is an array
$form->bind($this->createPostRequest(), array());
} }
public function testBindThrowsExceptionIfNoValidatorIsSet() public function testBindThrowsExceptionIfNoValidatorIsSet()