Only add/display constraint(s) which match the used validation group(s) #6130
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Now that we finally can use as many validation groups in forms as we want (#6124), there is one thing I would consider a bug.
Right now, when you retrieve a field from a form (via
myForm.field("fieldname")), we always return the field with all of its constraints, no matter if we told the form to use a specific validation group or not.This is wrong.
If you create a form and tell it to use (a) specific validation group(s) then only the constraints should be added (to a field) which match any of the validation groups the form actually currently uses.
For example if you display all fields of a form in a view, and all fields contain the
@Requiredconstraint, but some of this constraints use a specific validation group like@Required(groups = MyGroup.class)and you create the form viaformFactory.form(myForm.class, MyGroup.class)then in the view only the fields should display the required constraint ("This field is required") who's required annotation actually matches the group - because of course all other fields are actually not required for the current form submission scenario (and of course don't raise a validation error if they are empty on submission).Right now always all constraints get added/shown.
Fortunately we can make use of the
unorderedAndMatchingGroupsmethod which does exactly what we want. (javadoc) More on it's usage can also be found in the hibernate validation docs here and the Bean Validation specification here (just grep forunorderedAndMatchingGroups)One more thing: When no groups are used, we still need to supply the
Default.classgroup, because that's what is getting used by default by the.validate(...)method we use here:(From the javadocs [here](http://docs.jboss.org/hibernate/beanvalidation/spec/1.1/api/javax/validation/Validator.html#validate%28T, java.lang.Class...%29))
We already use the change of this PR in our own custom Play
2.5.4-SNAPSHOTbuild since two days and it is working like a charm.