šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Book a DemoInstallSign in
Socket

react-form-helper-validator

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-form-helper-validator

⚔ Tiny library for form state/ validation written entirely with React Hooks

1.0.10
latest
Source
npm
Version published
Weekly downloads
8
-65.22%
Maintainers
1
Weekly downloads
Ā 
Created
Source

react-form-helper-validator

⚔ Tiny library for form state/ validation written entirely with React Hooks

Installation

npm install react-form-helper-validator --save

Or

yarn add react-form-helper-validator

Usage

First off let's import FormHelper

import  FormHelper  from  "react-form-helper-validator";

and then wrap your form with FormHelper like so while providing required props

  const model = {
    email: "",
  };

  const rules = {
    email: [
      v => !v && "This field is required",
      v => validationRules.emailValidation(v, "email")
    ]
  };

<FormValidator model={model} rules={rules}>
      {({
        validate,
        formData,
        formState,
        formErrors,
        update,
        resetForm,
        hasErrors,
        validateField,
        clearValidation
      }) => {
        const handleSubmit = e => {
          e.preventDefault();
          validate()
            .then(() => {
              console.log("submitting", formData);
            })
            .catch(err => {
              console.error("err", err);
            });
        };
        return (
            <form onSubmit={handleSubmit}>
                <div className="field">
                  <label className="label">
                    Email
                  </label>
                  <div className="control">
                    <input
                      name="email"
                      value={formData.email}
                      onChange={update.text}
                      className="input"
                      type="email"
                      placeholder="Enter your email"
                    />
                  </div>
                  <p
                    className={`help ${!formErrors["email"] &&
                      "is-success"} ${formErrors["email"] && "is-danger"}`}
                  >
                    {formErrors["email"]}
                  </p>
                </div>
                <button
                  onClick={handleSubmit}
                  type="submit"
                  className="button"
                >
                  Submit
                </button>
            </form>
        )
    }
</FormValidator>

Props

PropRequiredTypePurpose
modeltrueObjectProvides initial form data
rulesfalseObjectAn object containing an array of rules which corresponds to data properties. Rules are functions that return an error string on error and false otherwise
manualfalseArrayAn array of field names which shall not be validated automatically on change

Keywords

react

FAQs

Package last updated on 29 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts