10000 add allow_extra_fields option · tommygnr/symfony@c6f3dd6 · GitHub
[go: up one dir, main page]

Skip to content

Commit c6f3dd6

Browse files
committed
add allow_extra_fields option
1 parent 430c6d7 commit c6f3dd6

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function validate($form, Constraint $constraint)
107107
}
108108

109109
// Mark the form with an error if it contains extra fields
110-
if (count($form->getExtraData()) > 0) {
110+
if (!$config->getOption('allow_extra_fields') && count($form->getExtraData()) > 0) {
111111
$this->context->addViolation(
112112
$config->getOption('extra_fields_message'),
113113
array('{{ extra_fields }}' => implode('", "', array_keys($form->getExtraData()))),

src/Symfony/Component/Form/Extension/Validator/Type/FormTypeValidatorExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
6767
'cascade_validation' => false,
6868
'invalid_message' => 'This value is not valid.',
6969
'invalid_message_parameters' => array(),
70+
'allow_extra_fields' => false,
7071
'extra_fields_message' => 'This form should not contain extra fields.',
7172
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
7273
));

src/Symfony/Component/Form/Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,28 @@ public function testViolationIfExtraData()
615615
$this->validator->validate($form, new Form());
616616
}
617617

618+
public function testNoViolationIfAllowExtraData()
619+
{
620+
$context = $this->getMockExecutionContext();
621+
622+
$form = $this
623+
->getBuilder('parent', null, array('allow_extra_fields' => true))
624+
->setCompound(true)
625+
->setDataMapper($this->getDataMapper())
626+
->add($this->getBuilder('child'))
627+
->getForm();
628+
629+
$form->bind(array('foo' => 'bar'));
630+
631+
$context->expects($this->never())
632+
->method('addViolation');
633+
$context->expects($this->never())
634+
->method('addViolationAt');
635+
636+
$this->validator->initialize($context);
637+
$this->validator->validate($form, new Form());
638+
}
639+
618640
/**
619641
* @dataProvider getPostMaxSizeFixtures
620642
*/

0 commit comments

Comments
 (0)
0