From 23a71e59e104a96e5a868e83eb2a87f87c46d34f Mon Sep 17 00:00:00 2001 From: entering Date: Sun, 7 Jul 2013 00:46:16 +0100 Subject: [PATCH] [Form] Validation listener remove count() --- .../EventListener/ValidationListener.php | 12 +++++------ .../EventListener/ValidationListenerTest.php | 20 +++++++++++++++++++ .../Type/FormTypeValidatorExtensionTest.php | 14 +++++++++---- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php b/src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php index 1414753153..26d2bfdaa7 100644 --- a/src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php +++ b/src/Symfony/Component/Form/Extension/Validator/EventListener/ValidationListener.php @@ -54,14 +54,12 @@ class ValidationListener implements EventSubscriberInterface // Validate the form in group "Default" $violations = $this->validator->validate($form); - if (count($violations) > 0) { - foreach ($violations as $violation) { - // Allow the "invalid" constraint to be put onto - // non-synchronized forms - $allowNonSynchronized = Form::ERR_INVALID === $violation->getCode(); + foreach ($violations as $violation) { + // Allow the "invalid" constraint to be put onto + // non-synchronized forms + $allowNonSynchronized = Form::ERR_INVALID === $violation->getCode(); - $this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized); - } + $this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized); } } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php index 528f94633b..57e0a48185 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/EventListener/ValidationListenerTest.php @@ -17,6 +17,7 @@ use Symfony\Component\Form\Extension\Validator\Constraints\Form; use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener; use Symfony\Component\PropertyAccess\PropertyPath; use Symfony\Component\Validator\ConstraintViolation; +use Symfony\Component\Validator\ConstraintViolationList; class ValidationListenerTest extends \PHPUnit_Framework_TestCase { @@ -142,4 +143,23 @@ class ValidationListenerTest extends \PHPUnit_Framework_TestCase $this->listener->validateForm(new FormEvent($form, null)); } + + public function testValidateWithEmptyViolationList() + { + $form = $this->getMockForm(); + $form->expects($this->once()) + ->method('isRoot') + ->will($this->returnValue(true)); + + $this->validator + ->expects($this->once()) + ->method('validate') + ->will($this->returnValue(new ConstraintViolationList())); + + $this->violationMapper + ->expects($this->never()) + ->method('mapViolation'); + + $this->listener->validateForm(new FormEvent($form, null)); + } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php index 6619410599..a859b49998 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Type/FormTypeValidatorExtensionTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Form\Tests\Extension\Validator\Type; use Symfony\Component\Form\FormInterface; +use Symfony\Component\Validator\ConstraintViolationList; class FormTypeValidatorExtensionTest extends TypeTestCase { @@ -69,15 +70,20 @@ class FormTypeValidatorExtensionTest extends TypeTestCase public function testSubmitValidatesData() { - $builder = $this->factory->createBuilder('form', null, array( - 'validation_groups' => 'group', - )); + $builder = $this->factory->createBuilder( + 'form', + null, + array( + 'validation_groups' => 'group', + ) + ); $builder->add('firstName', 'form'); $form = $builder->getForm(); $this->validator->expects($this->once()) ->method('validate') - ->with($this->equalTo($form)); + ->with($this->equalTo($form)) + ->will($this->returnValue(new ConstraintViolationList())); // specific data is irrelevant $form->submit(array());