Table of Contents generated with DocToc
- React Form Validation
- Installation
Wrapper component with ability to validating any user input with various available rules using render props pattern. Inspired by laravel validation rules
Using Npm
npm install react-form-validation-render-prop
Using Yarn
yarn add react-form-validation-render-props
import React, { Component } from "react";
import { Form } from "react-form-validation-render-props";
class App extends Component {
state = {
email: "",
password: ""
};
// rules using array
// rules = {
// email: ["required", "email"],
// password: ["required", "between:5:10"]
// };
// rules using string
// rules = {
// email: 'required|email',
// password: 'required|between:5:10'
// }
// rules using object and custom global message and custom message
rules = {
email: {
rules: ["required", "email"],
message: "Please allow me to fill your inbox"
},
password: {
rules: "required|between:5:10",
message: {
required: "Allow yourself to come to our system",
between: "Make yourself secure"
}
}
};
onChangeValue = (key, value) => {
this.setState({ [key]: value });
};
onSubmit = (e, valid) => {
e.preventDefault();
console.log(valid);
};
render() {
return (
<Form
data={this.state}
rules={this.rules}
onSubmit={this.onSubmit}
onChangeValue={ths.onChangeValue}
>
{({ isValidate, errors, onChange, data, onSubmit }) => {
return (
<form onSubmit={onSubmit}>
{data.map(item => {
return (
<div key={item.key}>
<label>{item.key}</label>
<input type="text" onChange={onChange(item.key)} />
{!isValidate && (
<span className="has-tex-error">
{errors.get(item.key)}
</span>
)}
</div>
);
})}
<button type="submit">Masuk</button>
</form>
);
}}
</Form>
);
}
}
export default App;
Name | Type | Description | Example | Parameters |
---|---|---|---|---|
data | Object |
Data that will be validated | data={email: '[email protected], fullName: 'semmi verian} |
|
rules | Object/ String / Array |
Collection of rules that will run the validation | More Detail | |
onChangeValue | function |
Function that will be called whenever internal onChange function is triggered |
the key and the value of what data is being updated. |
1. key => the key of data which is being updated 2. value => the updated value |
onSubmit | function |
Function that will be called whenever interval onSubmit function is triggered |
onSubmit=function onSubmit (e, valid) { // code here } |
1. e => event that being triggered 2. valid => flag that will tell whether the validation is passed or failed |
children | function |
Children function that will be rendered whatever view you want with utilities given from the library | children=({ isValidate, errors, onChange, data, onSubmit }) => {}) Link ke detail |
More Detail |
validateOnChange | boolean |
Flag that tell library to validate user input whenever internal onChange function is triggered default to true |
validateOnChange={true} |
By Using Children function you have 100% authority to use whatever style you want that will be apply to this library.
There are two options for you to define the Children props. You can choose what ever style you prefer to define the children function.
The First Option
<Form
children={({ isValidate, errors, onChange, data, onSubmit }) => {
return (<form></form>)
}}></Form>
The Second Option
<Form>
{({ isValidate, errors, onChange, data, onSubmit }) => {
return <form />;
}}
</Form>
In this section we will cover each argument that available inside our children function
Example
<Form>
{({ isValidate, errors, onChange, data, onSubmit }) => {
return <form />;
}}
</Form>
is an Array
that you can use to reference what data is available for you to render.
data structure
[
{ key: "email", value: "[email protected]" },
{ key: "fullName", value: "semmi verian putera" }
];
example
{
({ isValidate, errors, onChange, data, onSubmit }) => {
return (
<form onSubmit={onSubmit}>
{data.map(item => {
return (
<div key={item.key}>
<label>{item.key}</label>
<input type="text" onChange={onChange(item.key)} />
{!isValidate && (
<span className="has-tex-error">{errors.get(item.key)}</span>
)}
</div>
);
})}
<button type="submit">Masuk</button>
</form>
);
};
}
In above example we use the data
arguments to map all available data from our inner component to render at consumer component.
is a boolean
to tell you whether all the validation rules is success or failed
is a Class
that will tell you what are the errors for the failed validation
to get the errors for the given key you can use our function like this
<span className="has-tex-error">{errors.get(item.key)}</span>
it will return whatever error message that exist at the given key.
is a function
that you should apply to let our internal component know what changes from the consumer component and will trigger the validation rules if you set the validateOnChange
flag to true
. Whenever this function is called our internal component will trigger a props function onChangeValue
so you can update your state whenever the input is changing. This function need one arguments which is the key will be updated by the new value. In the above example if we define "email"
as the argument then our component will update the "email"
value at our internal component state.
example
<input type="text" onChange={onChange("email")}
is a function
that will trigger our validation to run to every data. Whenever it called it will trigger the props onSubmit
with two arguments e
and valid
.
example
{
({ isValidate, errors, onChange, onSubmit }) => {
return <form onSubmit={onSubmit} />;
};
}
every rules need an object with [key]: [value]
pair like this example.
rules = {
email: ["required", "email"],
password: ["required", "between:5:10"]
};
data = {
email: "[email protected]",
password: "secret"
}
<Form data={data} rules={rules}></Form>
the key email
at rules object will be used to find the value at data object to be validate.
so in the above example the rule required and email
in the email
key at rules object will be validate against [email protected]
in the email
key at data object.
Some Validation rules need more arguments beside the data
to be working properly, you can defining arguments in every rules type Array|String|Object
by using separator :
the first encounter :
will be first arguments and so on and so forth.
Example
// between validation
{
password: "between:5:10";
}
in above example 5 will be the first argument and define as variable min
in our between
validation rule and 10 will be the second argument and define as variable max
in our between
validation rule.
You can use Array as your rules collection like this
rules = {
email: ["required", "email"],
password: ["required", "between:5:10"]
};
You can use String as your rules collection like this. You can use multiple validation rule for one data by using separator |
rules = {
email: "required|email",
password: "required|between:5:10"
};
By using Object you will have to define two key for the rule working properly the first one is rule
that will tell what rule
should be implemented and the other will be the message that will overwrite the default error message
{
email: {
rules: ["required", "email"],
message: "Please allow me to fill your inbox"
}
}
by using String
as the message
value that string will override every fail validation rules.
{
password: {
rules: "required|between:5:10",
message: {
required: "Allow yourself to come to our system",
between: "Make yourself secure"
}
}
}
by using Object
as the message
value the error message will be override the key provided by user.
as default required error message is
field is required
but if you are using the custom message for the required (key) the error will be override with the given message.
Allow yourself to come to our system
[key] is the given data name
say your validation rule is an object like this
rules = {
email: ["required", "email"],
password: ["required", "between:5:10"]
};
then if your email validation is failed in any rules that fail the [key]
at yout error message will be replace with email
Name | Extra Parameter(s) | Default Error Message | Invalid Condition |
---|---|---|---|
required | - | [key] field is required | 1. empty array 2. Empty Object 3. null or empty data |
- | [key] field is invalid email address | Invalid email address with regex validation | |
number | - | [key] field is not a numeric value | Not a Number NaN |
url | - | [key] field is not a valid Url | Invalid Url with regex validation |
max | {max} | [key] field cannot exceed {max} characters | Data (string) characters length is exceed the given maximum character |
min | {min} | [key] field must be at least {min} characters | Data (string) characters length is less than the given minimun character |
lessThan | {max} | [key] field must be less than {max} | Data (number) is greater than the given maximum number |
greaterThan | {min} | [key] field must be greater than {max} | Data (number) is less than the given minimum number |
between | {min}, {max} | [key] field must be at least {min} character and not exceed {max} characters | Data (String) characters length is less than the given minimum character and greater than the given maximum character |
date | - | [key] field is an invalid Date | Invalid Date with javascript built in date validation |
ifExist | {otherField} , {otherRule}, {...rest} | the error message will be the same like the {otherRule} validation message | When the {otherField} is exist and fail the {otherRule} validation rules. |
ifDoesntExist | {otherField}, {otherRule}, {...rest} | the error message will be the same like the {otherRule} validation message | When the {otherField} is not exist and fail the {otherRule} validation rules. |
whenTrue | {condition}, {otherRule}, {...rest} | the error message will be the same like the {otherRule} validation message | when the {condtion} given is true as a String and fail the {otherRule} validation rules |
whenFalse | {condition}, {otherRule}, {...rest} | the error message will be the same like the {otherRule} validation message | when the {condtion} given is false as a String and fail the {otherRule} validation rules |
inArray | {arrays} | [key] is not included in this array {params} | Data given is not included in the {arrays} arguments. |
startsWith | {startWith} | [key] must be start with one of this characters {params} | Data given is not start with the given {startWith} arguments. |
endsWith | {endsWith} | [key] must be end with one of this characters {params} | Data given is not start with the given {endsWith} arguments. |
- [] Writing Test
- Kent C Dods - Advance React Component
- Jeffrey Way - Form Validation at vue
- Laravel Validation - Laravel Validation