jquery-validation / jquery-validation

jQuery Validation Plugin library sources

Home Page:https://jqueryvalidation.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request - set messages/rules for group

eden-jh opened this issue · comments

It would be great to have the ability to set messages and rules for a group, instead of just individual fields. The purpose of a group is to use a single message, but messages can still only be set at the individual field level.

I'm not married to these specifics, but here's what I'm thinking of:

  • The value of each key in groups can (optionally) be an object
  • This object contains keys for fields (space-separated string of field names that are members of this group), messages, and rules
  • For messages and rules, they take effect for each member of the group, unless overridden for an individual field

For example:

groups: {
   groupName: {
      fields: "fieldOne fieldTwo fieldThree fieldFour"
      rules: {
         minlength: 2,
         required: true
      }
      messages: {
         minlength: functionForMinLength
         required: functionForRequired
      }
}

The callbacks passed to messages can use the validator object to check which fields in the group currently have an error, and update the message accordingly.

To continue the previous example, if you wanted fieldFour to not be required, you would set that in the individual field rules to override the group setting:

rules: {
   fieldFour: {
      required: false
   }
}

Initially I imagined this as just a shorter way of applying rules and messages to individual fields in a group. In practice, this means the message is updated for each field in the group as it is validated, rather than waiting until all fields in the group are validated. So a message is potentially updated multiple times in a row. It may be worth considering the latter, but I realize that could be problematic.

This issue/proposal has been automatically marked as idle and stale because it hasn't had any recent activity. It will be automatically closed if no further activity occurs. If you think this is wrong, or the problem still persists, just pop a reply in the comments and one of the maintainers will (try!) to follow up.
Thank you for contributing :)