Denotes the form is valid or not
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ email: "", password: "" });
useEffect(() => {
if (form.isValidForm) {
console.log(fields, errors, form);
// Perform the api call here
}
}, []);
<form onSubmit={form.handleSubmit}>
</form>
Get the raw object of messages for the given language.
import { Lang, useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ customer_name: "" });
const messages = form.getMessages(Lang.en); (i.e) 'en'
console.log(messages);
Example in Code Sandbox.
Retrive Lang code validation error messages.
A method to handle the onblur event for input in the form.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ email: "" });
<input
type="text"
name="email"
value={fields.email}
onChange={form.handleChangeEvent}
onBlur={form.handleBlurEvent}
>
Handle onchange event for input fields.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ email: "" });
<input
type="text"
name="email"
onChange={form.handleChangeEvent}
value={fields.email}
>
A method to handle the react form submission.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ email: "", password: "" });
<form onSubmit={form.handleSubmit}>
</form>
Register Custom Validation Rules.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ phone: "required|telephone" });
form.register('telephone', function(value, requirement, attribute) {
return value.match(/^\d{3}-\d{3}-\d{4}$/);
}, 'The :attribute phone number is not in the format XXX-XXX-XXXX.');
Example in Code Sandbox
The name of the rule.
Returns a boolean to represent a successful or failed validation.
An optional string where you can specify a custom error message. :attribute inside errorMessage will be replaced with the attribute name.
Register an asynchronous rule which accepts a passes callback.
The data-async
custom attribute should be added in the html element.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ customer_name: "" }, { customer_name: "required|username_available" });
form.registerAsync('username_available', function(username, attribute, req, passes) {
// do your database/api checks here etc
// then call the `passes` method where appropriate:
passes(); // if username is available
passes(false, 'Username has already been taken.'); // if username is not available
});
<input
type="text"
name="customer_name"
onChange={form.handleChangeEvent}
onBlur={form.handleBlurEvent}
value={fields.customer_name}
data-async
>
Example in Code Sandbox.
The name of the rule.
An optional string where you can specify a custom error message. :attribute inside errorMessage will be replaced with the attribute name.
You can supply global custom attribute names in your app with the attributes property.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ customer_name: "" });
form.setAttributeFormatter(function(attribute) {
return attribute.replace(/_/g, ' ');
});
Example in Code Sandbox.
A Callback function to configure the attribute name.
You can also add your own custom language by calling setMessages.
import { Lang, useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ customer_name: "" }, { customer_name: "required" });
form.setMessages(Lang.en, {
required: 'The :attribute can't be empty.'
});
Example in Code Sandbox.
Override Lang code validation error messages.
A error messages object.
Set the validation rules for form fields. Find the available rules here.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ email: "", password: "" });
form.useRules({
email: "required|email",
password: "required"
});
The rules to validate.
The validate method is used to trigger the validator manually. It returns a promise function, it will resolve with boolean value. If the form doesn't have any errors it will resolve with true.
import { useFormInputValidation } from "react-form-input-validation";
const [fields, errors, form] = useFormInputValidation({ email: "", password: "" });
const onSubmit = async (event) => {
const isValid = await form.validate(event);
if (isValid) {
console.log(fields, errors);
// Perform the api call here
}
}
<form onSubmit={onSubmit}>
</form>
Example in Code Sandbox.
Hook form instance