Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator][GroupSequences] GroupSequences execute next group if first fail #22373

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
devpetrov opened this issue Apr 11, 2017 · 6 comments
Closed

Comments

@devpetrov
Copy link

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 2.8.19

This issue appears when form is used without entity/data_class. Both groups "Basic' and 'Strict' are validated even if there are violations in 'Basic'.

I also get the error: The Symfony\Component\Validator\Constraints\GroupSequence::getIterator method is deprecated since version 2.5 and will be removed in 3.0. in Symfony\Component\Validator\Constraints\GroupSequence.php on line 107 which is caused in by Symfony\Component\Form\Extension\Validator\Constraints\FormValidator.php:(84)

The Form and Validator components are working standalone outside of symfony distribution, with all the dependencies also available.

Here is how the form is configured:

class MyType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add("name", null, [
                    "constraints" => new NotBlank(["groups" => ["Basic"]])
                ])
                ->add("phone", MyPhoneType::class, [
                    "constraints" => [
                        new NotBlank(["groups" => ["Basic"]]),
                        new PhoneNumber(["groups" => ["Strict"])
                    ]
                ])
                ->add("email", EmailType::class, [
                    "constraints" => [
                        new NotBlank(["groups" => ["Basic"]]),
                        new Email(["groups" => ["Strict"]]),
                    ],
                ])
            ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            "validation_groups" => new GroupSequence(["Basic", "Strict"])
        ]);
    }
}
@gustavodpont
Copy link

gustavodpont commented Apr 11, 2017

Looking through the code
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Validator/RecursiveContextualValidator.php#L813

When mapping the constraints on the Form, $context->getViolations() is always a empty array, even when there's a violation. And mapping the constraints on the Entity it come with one position when there's a violation.

@xabbuh
Copy link
Member

xabbuh commented Apr 12, 2017

It would be nice if one of you could upload an example project that one could just checkout and execute to try to reproduce your issue.

@gustavodpont
Copy link

Sure, https://github.com/gustavodpont/issue22373
URL: /my/new

On this example there's 2 constraints for property name, Length (min => 10, groups => Basic) and one Email (groups => Strict) and then the validation group is a GroupSequence (["Basic", "Strict"]), mapped on the Form.
When you put something like "abcd" on name, expected is "This value is too short. It should have 10 characters or more." without the "This value is not a valid email address.", but the 2 messages appears.

@devpetrov
Copy link
Author

@gustavodpont thanks! :)

@pedrocasado
Copy link
Contributor

same with Symfony 3.3.2.

@HeahDude
Copy link
Contributor

@gustavodpont Thanks a lot for this reproducer! I think I found a way to fix this.

Could you please give #23722 a look?

ogizanagi added a commit that referenced this issue Sep 18, 2017
…Dude)

This PR was merged into the 2.7 branch.

Discussion
----------

[Form] Fixed GroupSequence with "constraints" option

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

Commits
-------

e39e7a4 [Form] Fixed GroupSequence with "constraints" option
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

7 participants