8000 [Form] Duplicated errors for constraints when using several validation groups · Issue #4427 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Duplicated errors for constraints when using several validation groups #4427

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

Closed
stof opened this issue May 26, 2012 · 15 comments
Closed

Comments

@stof
Copy link
Member
stof commented May 26, 2012

When adding a violation error on an unmapped field, the ViolationMapper creates a PropertyPath with an empty string, which is invalid and throws an exception.
Code to reproduce the issue:

<?php
use Symfony\Bundle\SecurityBundle\Validator\Constraint\UserPassword;

        $builder->add('current_password', 'password', array(
            'mapped' => false,
            'constraints' => new UserPassword(),
        ));
@stof
Copy link
Member Author
stof commented May 26, 2012

this is really weird. When trying again, I don't receive the exception anymore, but I have a duplicated error message

@stof
Copy link
Member Author
stof commented May 26, 2012

actually the validation seems to be called twice even without mapped => false. @bschussek do you have an idea about this ?

@webmozart
Copy link
Contributor

Can you please check whether #4431 fixes this?

@stof
Copy link
Member Author
stof commented May 28, 2012

It does not fix the issue. Btw, by putting a dump call in the validator, I saw that the validation is called twice.

@webmozart
Copy link
Contributor

Ok. Can you give me any instructions to reproduce this?

@stof
Copy link
Member Author
stof commented May 28, 2012

I found the issue when replacing our custom password validation in FOSUserBundle by the Symfony 2.1 constraint. I just pushed my password_validation branch in my fork of FOSUserBundle (the valdiation is used for the change-password form and the profile form).

@webmozart
Copy link
Contributor

I extracted ProfileFormType into a test project and replaced the UserPassword constraint by MinLength. The validator is called exactly once. Are you sure you're not binding twice?

@stof
Copy link
Member Author
stof commented May 28, 2012

The only place using the UserPassword constraint is the form type. And other constraints (coming from the Validator mapping) are not duplicated for the same form.

@webmozart
Copy link
Contributor

If you replace UserPassword by MinLength, is it also validated twice?

@stof
Copy link
Member Author
stof commented May 28, 2012

yes, MinLength also duplicates the error message when applying it on the unmapped field

@webmozart
Copy link
Contributor

I fear you have to debug it then. I cannot reproduce.

@stof
Copy link
Member Author
stof commented Jun 16, 2012

@bschussek I looked at the code and I found the issue: it is not only for unmapped fields but for all fields when using the constraints option (I was simply not using the option elsewhere). If you have several validation groups applied for your form, the constraint is applied in each of them, thus leading to duplicated errors.

@stof
Copy link
Member Author
stof commented Jun 16, 2012

The related code is https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php#L69
I don't know if we should simply remove the loop over groups or if there is a valid reason to use the groups there

fabpot added a commit that referenced this issue Jul 9, 2012
Commits
-------

c0a5207 [Form] Prevented duplicate validation of form constraints

Discussion
----------

[Form] Prevented duplicate validation of form constraints

Bug fix: yes
Feature addition: no
Backwards compatibility break: *yes* (at least that can be argued. I consider it a correction of incorrectly implemented functionality)
Symfony2 tests pass: yes
Fixes the following tickets: #4427
Todo: -
@fabpot fabpot closed this as completed Jul 9, 2012
@trsteel88
Copy link
Contributor

Was this resolved?

I have the following validation:

Vivo\PageBundle\Entity\Page:
    properties:
        pageTitle:
            - NotBlank:
                groups:
                    - base_validation
                    - page_model
            - Blank:
                groups:
                    - base_validation
                    - page_model

Form:

...
'validation_groups' => array('base_validation', 'page_mode')
...

I get duplicate error messages. Is this related?

page:
    pageTitle:
        ERROR: This value should be blank.
        ERROR: This value should be blank.

@hacfi
Copy link
Contributor
hacfi commented Aug 7, 2013

@trsteel88 I’ve experienced the same behavior and I think that is normal (meaning it wouldn’t be classified as a bug).

What I do to avoid multiple errors is to only set one validation_group in the form class. But it should also be possible to just set one group in the validation mapping...so try only adding base_validation and not page_model in your yml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
0