-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Comments
you meant
|
@docteurklein Yes, typo, fixed. Thanks Flo! |
@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? |
@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 Implementation should be simple, if it is not a root form, and have |
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 |
@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. |
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. |
@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. 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] } |
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. |
Talk is cheap, show me the code:
I expected this code to be equivalent to:
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.
The text was updated successfully, but these errors were encountered: