Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form] form config constraints overwrites extra_fields cause constraint #27362

Closed
AlexeyDubinchak opened this issue May 24, 2018 · 2 comments
Closed

Comments

@AlexeyDubinchak
Copy link

AlexeyDubinchak commented May 24, 2018

Symfony version(s) affected: >=3.4.5

Description
forms with disabled 'allow_extra_fields' and with defined constraints in options return wrong constraint in error cause

How to reproduce

 class NoExtraFieldsType extends AbstractType
 {
     ...
     public function configureOptions(OptionsResolver $resolver)
     {
         $resolver->setDefaults([
             'allow_extra_fields' => false,
             'csrf_protection' => false,
             'constraints' => [new SomeConstraint()]
         ]);
     }
 }
$form = $this->createForm(NoExtraFieldsType::class);
$form->submit([
    'existing_field' => 12345,
    'not_allowed_extra_field'
]);

foreach ($form->getErrors() as $error) {
    print_r([
        'message' => $error->getMessageTemplate(),
        'cause' => get_class($error->getCause()->getConstraint())
    ]);
}

<=3.4.4 (correct)

Array
(
[message] => This form should not contain extra fields.
[cause] => Symfony\Component\Form\Extension\Validator\Constraints\Form
)

>=3.4.5 (incorrect)

Array
(
[message] => This form should not contain extra fields.
[cause] => AppBundle\Validator\Constraints\SomeConstraint
)

Here is a repository with a reproduction of the problem : https://github.com/AlexeyDubinchak/symfony-formerror-issue

Possible Solution
Symfony\Component\Form\Extension\Validator\Constraints\FormValidator -> validate method

my guess problem is form validator runs this method in a loop for each constraint and is overwriting constraint property for cause

/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php#126
        // Mark the form with an error if it contains extra fields
        if (!$config->getOption('allow_extra_fields') && count($form->getExtraData()) > 0) {
            $this->context->setConstraint($constraint);

caused because of #25940

@jmalinens
Copy link

+1

@xabbuh
Copy link
Member

xabbuh commented Feb 2, 2019

Thank you for reporting. This will be fixed by #30062.

nicolas-grekas added a commit that referenced this issue Feb 7, 2019
…bbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] do not overwrite the constraint being evaluated

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27362
| License       | MIT
| Doc PR        |

Commits
-------

345a632 do not overwrite the constraint being evaluated
@xabbuh xabbuh closed this as completed Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants