0.1.4 • Published 3 years ago

@surinderlohat/lohat-react-form-validation v0.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Lohat React Form validation

Form validation solution for react JS, super easy to use and can handel all the major user cases.

Based on the React hooks and reactive programing.

No extra dependencies pure react js code & lightweight.

Features

  1. Easy validations solution for react js.
  2. Regex validations.
  3. Custom validations.
  4. Email validations.
  5. Many useful form helping methods i.e hasError, hasChanges, isTouched, getValues, getErrors.
  6. Easy to configure.
  7. Support complex user cases.
  8. Nested form fields validations.
  9. Dynamic add/remove fields.
  10. Open source Free to use.

Installation

npm install @surinderlohat/lohat-react-form-validation
or
yarn add @surinderlohat/lohat-react-form-validation

API Documentation

https://surinderlohat.github.io/lohat-react-form-validation/

Form Helping Methods

Note: all methods are available like: form.onSubmit()

MethodPARAMSDESCRIPTION
resetToDefaultNoReset form state to default All fields will be reset to the default state
getFieldfieldKey i.e name or user.name if nested fieldReturn the specific field instance
getChangedFieldsNoReturn fields with initial value and changed value
clearFormNoClear form values and all nested child's
onSubmitNoReturn from values and error state

Form Getters

NameRETURN TypeRETURN VALUE
getValuesobjectReturn form values in same order we have pass
getErrorsobjectDisplay all errors for each nested field
hasChangesbooleanReturn true if any field has changes

Field Methods

MethodTYPERETURN VALUE
hasChangesGetterReturn field state i.e it's changed or not
hasErrorGetterReturn error state of the specific field
errorMessageGetterReturn error message for current field from
setValueFunctionSet value of specific field
getValueFunctionGet value of specific field
setRules(newRules: Rules)Update field rules on the fly
setErrorFunctionSet custom error message on specific field
customValidation(field: IFieldProps, form: LohatForm)return error message with from custom logic Click Me

Rules

More will added soon
 min?: number; // min value
 max?: number; // max value
 required?: boolean; // make field required
 regExp?: RegExp; // pass any custom regex validation
 isEmail?: boolean; // validate email address
 same?: string; // provide key to match value

How to use

import { FieldObject, useLohatForm } from '@surinderlohat/lohat-react-form-validation';
const fields: FieldObject = {
  firstName: {
    label: 'First Name',
    value: 'Surinder',
    rules: {
      required: true,
      min: 2,
      max: 10,
    },
  },
 lastName: {
    label: 'Last Name',
    value: 'Singh',
    rules: {
      required: true,
      min: 2,
      max: 10,
    },
  },
  email: {
    label: 'Email',
    value: 'test@domain.com',
    rules: {
      isEmail: true,
      min: 2,
      max: 10,
    },
  },
};

function FormValidation() {
  const form = useLohatForm(fields);
  return (
    <div className="App">
      <form>
        <FormField field={form.getField('firstName')} />
        <FormField field={form.getField('lastName')} />
        <FormField field={form.getField('email')} />
        <Button onClick={()=>form.getValues()}>
        Save Changes
        </Button>
      </form>
    </div>
  );
}

function FormField({ field }: Props) {
  return (
    <TextField
      {...field.bind()} // find input methods to input i.e onChange, onBlur, onFocus
      label={field.starLabel}
      error={field.hasError}
      helperText={field.errorMessage}
      variant="filled"
    />
  );
}

Live working Examples

Basic validation with email validation

https://codesandbox.io/s/lohat-react-form-validation-o216l

Setup custom error message using form instance

https://codesandbox.io/s/lohat-react-form-validation-forked-ctejo

Nested form fields : Example include getValues() and getErrors() for specific nested field

https://codesandbox.io/s/lohat-react-form-validation-forked-tzmn3

Custom validation using customValidation function

https://codesandbox.io/s/lohat-react-form-validation-forked-8sv0x

License

MIT Free Software!

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago