From f993f8533d1f847432af71947fac6098ad89e0eb Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Fri, 25 Oct 2013 14:07:43 +0200 Subject: [PATCH 1/3] Allow extra fields in form option --- .../Form/Extension/Core/Type/FormType.php | 1 + .../Validator/Constraints/FormValidator.php | 2 +- .../Constraints/FormValidatorTest.php | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index a64b5ac465eb7..4cf51d64ffbc7 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php @@ -187,6 +187,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) // According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt) // section 4.2., empty URIs are considered same-document references 'action' => '', + 'allow_extra_fields' => false, )); $resolver->setAllowedTypes(array( diff --git a/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php b/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php index 8f02364e81f08..0cd00829e8077 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php +++ b/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php @@ -122,7 +122,7 @@ public function validate($form, Constraint $constraint) } // Mark the form with an error if it contains extra fields - if (count($form->getExtraData()) > 0) { + if (count($form->getExtraData()) > 0 && !$config->getOption('allow_extra_fields', false)) { $this->context->addViolation( $config->getOption('extra_fields_message'), array('{{ extra_fields }}' => implode('", "', array_keys($form->getExtraData()))), diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index a8bdde8a4d3a9..53e7ef039f25d 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -615,6 +615,25 @@ public function testViolationIfExtraData() $this->validator->validate($form, new Form()); } + public function testViolationIfExtraDataAllowed() + { + $context = $this->getMockExecutionContext(); + + $form = $this->getBuilder('parent', null, array('allow_extra_fields'=>true)) + ->setCompound(true) + ->setDataMapper($this->getDataMapper()) + ->add($this->getBuilder('child')) + ->getForm(); + + $form->submit(array('foo' => 'bar')); + + $context->expects($this->never()) + ->method('addViolation'); + + $this->validator->initialize($context); + $this->validator->validate($form, new Form()); + } + /** * @dataProvider getPostMaxSizeFixtures */ From 3fd5fb298642769bab878c3961202bf51dc4469d Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Fri, 25 Oct 2013 15:40:49 +0200 Subject: [PATCH 2/3] coding standards --- .../Validator/Constraints/FormValidatorTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index 53e7ef039f25d..b901a2c147fba 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -620,15 +620,15 @@ public function testViolationIfExtraDataAllowed() $context = $this->getMockExecutionContext(); $form = $this->getBuilder('parent', null, array('allow_extra_fields'=>true)) - ->setCompound(true) - ->setDataMapper($this->getDataMapper()) - ->add($this->getBuilder('child')) - ->getForm(); + ->setCompound(true) + ->setDataMapper($this->getDataMapper()) + ->add($this->getBuilder('child')) + ->getForm(); $form->submit(array('foo' => 'bar')); $context->expects($this->never()) - ->method('addViolation'); + ->method('addViolation'); $this->validator->initialize($context); $this->validator->validate($form, new Form()); From 193888a11ebc753d3f55b344cc6ed1a446dd1faa Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Mon, 11 Nov 2013 08:21:14 +0100 Subject: [PATCH 3/3] fixed test name for allow extra fields --- .../Tests/Extension/Validator/Constraints/FormValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php index b901a2c147fba..f3f401ea467a6 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php @@ -615,7 +615,7 @@ public function testViolationIfExtraData() $this->validator->validate($form, new Form()); } - public function testViolationIfExtraDataAllowed() + public function testNoViolationIfExtraDataAllowed() { $context = $this->getMockExecutionContext();