Hook form instance

Hierarchy

  • FormInstance

Properties

isValidForm: boolean

Denotes the form is valid or not

Example

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>

Methods

  • Get the default language being used.

    Example

    import { useFormInputValidation } from "react-form-input-validation";

    const [fields, errors, form] = useFormInputValidation({ customer_name: "" });
    form.getDefaultLang(); // returns e.g. 'en'

    Returns string

  • Get the raw object of messages for the given language.

    Example


    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);

    See

    Example in Code Sandbox.

    Parameters

    • langCode: Lang

      Retrive Lang code validation error messages.

    Returns object

  • A method to handle the onblur event for input in the form.

    Example


    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}
    >

    Parameters

    • event: FocusEvent<HTMLInputElement, Element>

    Returns void

  • Handle onchange event for input fields.

    Example


    import { useFormInputValidation } from "react-form-input-validation";

    const [fields, errors, form] = useFormInputValidation({ email: "" });

    <input
    type="text"
    name="email"
    onChange={form.handleChangeEvent}
    value={fields.email}
    >

    Parameters

    • event: ChangeEvent<HTMLInputElement>

    Returns void

  • A method to handle the react form submission.

    Example


    import { useFormInputValidation } from "react-form-input-validation";

    const [fields, errors, form] = useFormInputValidation({ email: "", password: "" });

    <form onSubmit={form.handleSubmit}>
    </form>

    Parameters

    • event: FormEvent<Element>

    Returns void

  • Register Custom Validation Rules.

    Example

    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.');

    See

    Example in Code Sandbox

    Parameters

    • name: string

      The name of the rule.

    • callbackFn: RegisterCallback

      Returns a boolean to represent a successful or failed validation.

    • errorMessage: string

      An optional string where you can specify a custom error message. :attribute inside errorMessage will be replaced with the attribute name.

    Returns void

  • Register an asynchronous rule which accepts a passes callback. The data-async custom attribute should be added in the html element.

    Example

    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
    >

    See

    Example in Code Sandbox.

    Parameters

    • name: string

      The name of the rule.

    • callbackFn: RegisterAsyncCallback
    • errorMessage: string

      An optional string where you can specify a custom error message. :attribute inside errorMessage will be replaced with the attribute name.

    Returns void

  • You can supply global custom attribute names in your app with the attributes property.

    Example

    import { useFormInputValidation } from "react-form-input-validation";

    const [fields, errors, form] = useFormInputValidation({ customer_name: "" });
    form.setAttributeFormatter(function(attribute) {
    return attribute.replace(/_/g, ' ');
    });

    See

    Example in Code Sandbox.

    Parameters

    • callbackFn: Function

      A Callback function to configure the attribute name.

    Returns void

  • You can also add your own custom language by calling setMessages.

    Example


    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.'
    });

    See

    Example in Code Sandbox.

    Parameters

    • langCode: Lang

      Override Lang code validation error messages.

    • values: object

      A error messages object.

    Returns void

  • Set the locale string for error messages.

    Example

    import { Lang, useFormInputValidation } from "react-form-input-validation";

    const [fields, errors, form] = useFormInputValidation({ name: "" });
    form.useLang(Lang.en);

    Parameters

    • locale: string

    Returns void

  • Set the validation rules for form fields. Find the available rules here.

    Example


    import { useFormInputValidation } from "react-form-input-validation";

    const [fields, errors, form] = useFormInputValidation({ email: "", password: "" });
    form.useRules({
    email: "required|email",
    password: "required"
    });

    Parameters

    • rules: any

      The rules to validate.

    Returns void

  • 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.

    Example


    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>

    See

    Example in Code Sandbox.

    Parameters

    • event: FormEvent<Element>

    Returns Promise<boolean>