Skip to content

Feature request - set messages/rules for group #2416

Closed
@eden-jh

Description

@eden-jh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleUsed to mark stale issues

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions