Force "required" prop on a field depending on its form's rules
See original GitHub issueWhat
Allow “required” to be set in a rule’s definition instead of form attribute
Why
I’m auto generating my validation rules and would like to keep the validation defined in one place. Having to maintain the “required” attribute on inputs could get out of sync and require extra maintenance.
How
authorizationKey: {
length: validation.isLengthBetween(8,14),
oneNumber: validation.hasOneNumber,
required: true <---
}
Keynotes
It would also be nice if it could maintain the same behavior as the attribute where the other rules for a field are not evaluated if the required rule is not met.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Client-side form validation - Learn web development | MDN
If the data entered in a form field follows all of the rules specified by the above attributes, it is considered valid.
Read more >React form validation solutions: An ultimate roundup
This roundup is a comprehensive look at some of the most popular solutions for form management and validation in React.
Read more >Form - Ant Design
High performance Form component with data scope management. Including data collection, verification, and styles.
Read more >eslint-plugin-react/prop-types.md at master - GitHub
This rule will validate your prop types regardless of how you define them. Rule Details. Examples of incorrect code for this rule: function...
Read more >Using the HTML5 "required" attribute for a group of checkboxes?
the browsers detects that the "required" field is empty and does not submit the form; instead browser shows a hint asking the user...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@T-Roth I’m glad I could help you.
If I may, there is a slightly lighter version of the same solution you’ve come up with:
I am confused why
fieldRules
returns the instance of ImmutableJS. I thinkif (fieldRules.get)
check is redundant. However, it’s beneficial, since we can use deep key refs by.getIn()
and omit unnecessary safety check.Sorry I didn’t get back to you sooner, your suggestion is working. Thanks for the help.
This is what I quickly came up with in case someone else wants to go this route (typescript):