Skip to content

Commit

Permalink
[Form] Fixed arrays not to be passed to the validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek committed Feb 2, 2011
1 parent 1c3e3f7 commit 7c9c7af
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
23 changes: 13 additions & 10 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -736,16 +736,19 @@ public function validate()
throw new MissingOptionsException('The option "validator" is required for validating', array('validator'));
}

// Validate the submitted data in the domain object in the sets
// validation group(s)
if ($violations = $validator->validate($this->getData(), $groups)) {
foreach ($violations as $violation) {
$propertyPath = new PropertyPath($violation->getPropertyPath());
$iterator = $propertyPath->getIterator();
$iterator->next(); // point at the first data element
$error = new DataError($violation->getMessageTemplate(), $violation->getMessageParameters());

$this->addError($error, $iterator);
// Only validate data if it is a domain object
if (is_object($this->getData())) {
// Validate the submitted data in the domain object in the sets
// validation group(s)
if ($violations = $validator->validate($this->getData(), $groups)) {
foreach ($violations as $violation) {
$propertyPath = new PropertyPath($violation->getPropertyPath());
$iterator = $propertyPath->getIterator();
$iterator->next(); // point at the first data element
$error = new DataError($violation->getMessageTemplate(), $violation->getMessageParameters());

$this->addError($error, $iterator);
}
}
}

Expand Down
26 changes: 22 additions & 4 deletions tests/Symfony/Tests/Component/Form/FormTest.php
Expand Up @@ -200,13 +200,13 @@ public function testValidationGroupsCanBeSetToArray()

public function testBindUsesValidationGroups()
{
$field = $this->createMockField('firstName');
$form = new Form('author', array(
'validation_groups' => 'group',
'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))
->method('validate');

Expand All @@ -221,8 +221,26 @@ public function testBindUsesValidationGroups()
// ->method('validate')
// ->with($this->equalTo($form));

// data is irrelevant
$form->bind($this->createPostRequest());
// concrete request is irrelevant
// 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()
Expand Down

0 comments on commit 7c9c7af

Please sign in to comment.