Gathering detailed insights and metrics for react-validate-form
Gathering detailed insights and metrics for react-validate-form
Gathering detailed insights and metrics for react-validate-form
Gathering detailed insights and metrics for react-validate-form
npm install react-validate-form
Typescript
Module System
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
190.9%
32
Compared to previous day
Last Week
35.5%
164
Compared to previous week
Last Month
6%
580
Compared to previous month
Last Year
-14.8%
6,230
Compared to previous year
Wraps any number of inputs using a function as a child component and validates them on either a set of built in rules or custom rules that can be passed in. The child of the Validation component must be a function and that function is passed several arguments to work with.
npm install react-validate-form
import Validate from "react-validate-form"
1 <Validate> 2 {({ validate, errorMessages }) => ( 3 <Fragment> 4 <input onChange={validate} name="first" required /> 5 <p>{errorMessages.first[0]}</p> 6 </Fragment 7 )} 8 </Validate>
The function that is called in the body of the Validate
component is passed on object that contains:
validate
:
a function that can be attached to any input event in order to validate that input. The event is passed through and the Validation component validates based on the rules passed in
errorMessages
:
an object of errorMessages. Keys are based on the inputs name
field. An input with name="first"
an access error messages from errorMessages.first
allValid
:
a boolean telling you if all fields attached to the validator are valid
errorCount
:
total error count
The above example calls the validate
function when the onChange event occurs on that input, but the validate
function can be triggered from any event that you can dream of.
Since the required
attribute exists on the input, any time the validate
function is fired the value of that input will be tested against that validation rule.
In the instance that the input fails validation then an object with the key of the input name
will exist inside of the errorMessages
object. In the above example, since the input has the attribute name="first"
an array of errors will exist on errorMessages.first
required
add to an input by either having the required
attribute or by passing in validation object prop ["required"]
email
add to an input by either having the type="email"
attribute or by passing in validation object prop ["email"]
min
add to an input by either having the min="3"
attribute or by passing in validation object prop ["min:3"]
max
add to an input by either having the max="5"
attribute or by passing in validation object prop ["max:5"]
validations
Another way to assign validation rules to inputs besides inline on the input is by passing in an object that maps the keys to an array of validation rules that are required for that input. The keys need to match the name
field on the inputs.
1 const validations = { 2 firstName: ["required"], 3 email: ["required", "email"], 4 password: ["required", "min:3", "max:15"], 5 } 6 7 <Validate 8 validations={validations} 9 > 10 ... 11 </Validate>
The validation rules are passed in as strings and MUST match either one of the built in rules or a custom defined rule
Some built in rules and any custom rules have the ability to take arguments. For example the built in rule for min:3
Passes the argument 3
into both the test
function and message
function.
Rules with arguments are :
min
max
Creating your own rules that take arguments will need to define both the test
function and message
function a bit differently.
See creating rules with arguments
rules
You are not limited to just the built in rules. You can create your own by passing in a rules object. The key
used to define each rule will be the string that is needed to assign your new rule to a validation.
A rule
is itself an object and must contain two properties:
test
a function that takes the value of the input being tested as an argument and return a boolean
. true
if the value is valid, false
if it is not.
message
a function that takes the both the name
of the input field and its current value, and returns a message. This is the error message that is returned in the event that the test
function returns false.
1 const rules = { 2 customRule: { 3 test: (val) => val.indexOf("cool") >= 0, 4 message: (field, val) => `${field} should contain the word cool. Check the value ${val}`, 5 }, 6 }; 7 8 const validations = { 9 firstName: ["required", "customRule"], 10 }; 11 12 <Validate 13 validations={validations} 14 rules={rules} 15 > 16 ... 17 </Validate>
You can also overwrite just a test
or message
of any built in rule by just passing in the function you need to replace
1 const rules = { 2 required: { 3 message: (field) => `${field} should be required and this message is custom`, 4 }, 5 }; 6 const validations = { 7 firstName: ["required"], 8 }; 9 10 <Validate 11 validations={validations} 12 rules={rules} 13 > 14 ... 15 </Validate>
A rule that accepts an argument like min:3
can also be created as a custom rule but with an extra step. You have to curry your functions to take the argument first and then return a function that takes the value.
1 customMin: { 2 test: (arg) => (val) => val.length >= arg, 3 message: (arg) => (name) => `${name} must be at least ${arg} characters.`, 4 },
No vulnerabilities found.