Skip to content
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

Additional Rules/Validators on Groups (Not just required) #124

Open
jaason435 opened this issue Jun 28, 2023 · 3 comments
Open

Additional Rules/Validators on Groups (Not just required) #124

jaason435 opened this issue Jun 28, 2023 · 3 comments

Comments

@jaason435
Copy link

jaason435 commented Jun 28, 2023

Is your feature request related to a problem? Please describe.
I need more validation on groups other than required. My use case (medical software) might be more complex than most, but we need the ability to add custom validators on groups to make sure that the selected value is valid in the context of other values in different fields.

Describe the solution you'd like
I think having a function "addGroup" that behaves just like "addField" would be great. Or, perhaps easier, is to add an "extraRules" parameter to "addRequiredGroup".

Btw, thanks for all your work creating this fantastic library.

@horprogs
Copy link
Owner

horprogs commented Jul 5, 2023

Hey thanks! What other rules would be useful for groups for you case?

@jaason435
Copy link
Author

That's a good question. I don't think any of the standard rules for single questions would apply. The main use case is adding a custom validator. We use these often in single fields to check values in other inputs to ensure that contradicting information isn't entered.

There are plenty of examples I could provide but a good one is blood type. Say a user selects blood type A in our radio button group. That's fine. When the user moves on to the blood subtype dropdown, I want to throw up an error if they try selecting "Not applicable" or "Unknown".

Maybe I'm missing something with groups, but I would benefit greatly from just being able to write custom validators alongside 'required' rules.

@silent-e
Copy link

I'm doing it successfully. Here's an example where I'm making sure a text input is both required and less than 100 characters in length. But we are only checking the length if a related <select> input is has had any choice but the first one which is often something like "Select one".

validator.addField('#field_id', [
  {
    rule: 'required',
    errorMessage: 'This field is required'
  },
  {
    validator: (value, context) => {
      if (document.getElementById('related_select').selectedIndex === 0) {
        return true; // default to valid if the select is still on the first choice
      }
      return value.length < 100;
    },
    errorMessage: 'error for this custom validator'
  }
])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants