8000 [DX][Form] Defining validation groups on form field level should throw an error · Issue #12342 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DX][Form] Defining validation groups on form field level should throw an error #12342

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
umpirsky opened this issue Oct 28, 2014 · 9 comments
Closed

Comments

@umpirsky
Copy link
Contributor

Talk is cheap, show me the code:

class MyFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('foo', 'text', [
            'constraints'       => new Constraints\Length(['min' => 6]),
            'validation_groups' => 'Bar',
        ]);
    }
 }

I expected this code to be equivalent to:

class MyFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('foo', 'text', [
            'constraints'  => new Constraints\Length(['min' => 6, 'groups' => 'Bar']),
        ]);
    }
 }

But it is not, validation groups are not respected in the first case.

If it cannot be fixed before 3.0, then it can at least throw an error like:

You cannot define validation groups to a form field, consider passing it as an option to your constraints.

@docteurklein
Copy link
Contributor

you meant

'constraints'  => new Constraints\Length(['min' => 6, 'groups' => 'Bar']),

@umpirsky
Copy link
Contributor Author

@docteurklein Yes, typo, fixed. Thanks Flo!

@webmozart
Copy link
Contributor

@umpirski Apparently you misunderstood how this option works. The "validation_groups" option is only supported on the root form where the validation is triggered. On fields and nested forms, this option is ignored.

The value of the option controls which validation group the form's data is validated in, i.e. (figuratively):

// $form is the root form
$data = $form->getData();
$groups = $form->getOption('validation_groups');

$violations = $validator->validate($data, null, $groups);

Does this answer your question?

@umpirsky
Copy link
Contributor Author

@webmozart I know. This have sense when you know that every field is a form as well, and that it is respected only when validation is triggered on that very form.

But DX I was talking about is that form field behaves same with or without 'validation_groups' => 'Bar' option. I want to say, if I can set validation_groups option on form field, then it is logical to expect it to be respected, and not ignored.

Implementation should be simple, if it is not a root form, and have validation_groups option set, throw error.

@webmozart
Copy link
Contributor
8000

Implementation should be simple, if it is not a root form, and have validation_groups option set, throw error.

To do that, we'd need to break BC. For example, if you hardcode the "validation_groups" option in a Type class and then embed that type in another form, you'll get an exception, where previously the option was simply ignored.

Do you think that's worth it?

In the long term, I think that the "validation_groups" option could be replaced by a new @DefaultGroup annotation for the Validator: #11880

@umpirsky
Copy link
Contributor Author
umpirsky commented Nov 3, 2014

@webmozart Well, I am not sure if it is worth it, I just felt it does not feel natural and decided to open new issue.

What is important is that we agree with this and that we are aware that problem exists. For me it is enough to fix it in 3.0.

@flip111
Copy link
Contributor
flip111 commented Nov 5, 2014

I'm not really in favor of any code that checks this on runtime during normal application operation. Possible there can be a seperate version of the Form class that has additional checks while debugging. Or a check can be performed during "compile time" (when generating cache takes place as well), or manually invoke some (static?) checker.

@hanneskaeufler
Copy link

@umpirski Apparently you misunderstood how this option works. The "validation_groups" option is only supported on the root form where the validation is triggered. On fields and nested forms, this option is ignored.

@webmozart Uh, this should be noted in the documentation maybe? I think this has cost me a few times.

Say you have a FormType with a collection field, whose entities you want to validate depending on their type? If you cannot set the validation_group in the child type, do you reach deep down into the collection from the root form to set the validation_groups?

See this example, validate based on submitted data with a closure.
http://symfony.com/doc/current/book/forms.html#groups-based-on-the-submitted-data

Or with tags

class Category {
    public $tags;
}
class Tag {
    const FOO_TYPE  = 1;
    const BAR_TYPE = 2;

    public $type;
    public $name;
}
class CategoryType extends AbstractFormType {
    ....
    $builder->add('tags', 'collection')
    ....

    function setDefaultOptions() {
         // set validation groups here???
    }
}
# tag.yml
name:
    - NotBlank: { groups: [TypeFooGroup] }

@xabbuh
Copy link
Member
xabbuh commented Oct 5, 2015

Closing here as I think we won't change the behaviour of the Form component's Validator extension.

However, the bahaviour of how the option is evaluated (only on the root form) is something that must be document to mitigate confusion. We have an issue for this in the docs repository (see symfony/symfony-docs#4401) and we will appreciate any contribution to fix that long standing issue.

@xabbuh xabbuh closed this as completed Oct 5, 2015
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

6 participants
0