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 - different errors in group

eden-jh opened this issue · comments

I need to show one error per validation method in the group, rather than one error per group. For example: "Field One and Field Two may not be longer than 10 characters." and "Field Three and Field Four are required", as separate but adjacent error elements, so that the aria-describedby on each field can point only to the appropriate message for each field. Currently all fields in a group are aria-describedby the same error, even if some of the fields in the group are valid. There needs to at least be an option to change this.

See related feature request for group messages and rules: #2416

I'm thinking this could be implemented as an option for backwards compatibility:

groups: {
    groupName: {
        fields: "fieldOne fieldTwo fieldThree fieldFour"
        groupByMethod: true
    }
}

I can think of multiple ways that the success setting could work here (is the message always blank on success, or is there ever any content that would necessitate an aria-describedby relationship to valid fields?), or we can decide that it's not compatible with this option.

Example

Demo steps

  1. Open the form demo
  2. In the First Name field, enter the digit 3
  3. Hit the tab key to move focus out of the First Name field
  4. Notice that an "Invalid format" message appears for the Name group
  5. Hit the Submit button to submit the form

Current behavior

There's one error message for the Name group, which says "This field is required." The "Invalid format" message is no longer there.

If you enter "Test" in the Middle Name and hit tab, the field is marked as valid but is still aria-describedby the error message.

Requested behavior

The "Invalid format" message and the required message are both displayed as adjacent elements. First Name is aria-describedby the format message. Middle Name and Last Name are aria-describedby the required message. (The message text should also be more user-friendly--"First Name cannot contain digits." and "Middle and Last Name are required."-- I just didn't implement that here for time reasons.)

If you enter "Test" in the Middle Name and hit tab, the field should be marked as valid, and it's aria-describedby attribute should be modified/removed (depending on whether there's another value) so it no longer points to the required message. (Also, in the user-friendly version of the message, the text is modified to "Last Name is required.")

I'd like to branch this feature off of PR #2410 (adding an option to remove aria-describedby when there's no longer an error) so that the aria-describedby for each error type can point specifically to the fields with that error, instead of having all the errors describe everything in the group. Ideally I'd also like to include #2418 because it'll be extremely tedious to pass the same message function separately for every field in a group.

If I were to keep this feature separate from the other two (or to branch it from just one of them), is it significantly more likely to be accepted? I need to have these features together either way (and plan to develop them), but I'd like to contribute if I can.

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 :)