Socket
Socket
Sign inDemoInstall

@surinderlohat/react-form-validation

Package Overview
Dependencies
3
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @surinderlohat/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.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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 pure react js no extra dependency.
  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. Nested form fields validations.
  8. Dynamic add/remove fields.

Installation

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

API Documentation

https://surinderlohat.github.io/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
isTouchedGetterif field is touched or not
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
showErrorsFunctionShow error without touching the fields

Rules

Rules 
    /* Used to identify which rule we are going to use for a field*/
    rule: 'min' | 'max' | 'required' | 'same' | 'email' | 'regex' | 'custom' | 'between' | 'range';
    /* Rule value used to provide value in rule i.e for min:2 max:10 rule value ruleValue will be ruleValue:2 or 5 
    ruleValue?: any;
    /* Used to provide custom message for each rule */
    message?: string;
    /* For special cases if we need custom validation then this method will help */
    validation?: (field: IField, form?: ReactForm) => string;

How to use

import { IFieldObject, useReactForm } from '@surinderlohat/react-form-validation';
import FormField from '../FormField/FormField';
import { Box, Typography, Button, Paper } from '@mui/material';
import { FC } from 'react';

// Fields Rules
const field: IFieldObject = {
  name: {
    label: 'User Name',
    placeholder: 'Enter your Name',
    rules: [{ rule: 'required' }],
  },
  email: {
    label: 'Email',
    placeholder: 'Enter your email',
    rules: [{ rule: 'email', message: 'Email is not valid' }],
  },
  password: {
    label: 'Password',
    rules: [{ rule: 'required', message: 'This field is required' }],
  },
  confirmPassword: {
    label: 'Confirm Password',
    rules: [{ rule: 'required' }, { rule: 'same', ruleValue: 'password', message: 'Should be same as Password' }],
  },
};

const BasicExample: FC = () => {
  const form = useReactForm(field);
  return (
    <Box sx={{ display: 'flex', justifyContent: 'center' }}>
      <Paper sx={{ width: '350px', padding: '15px', display: 'grid', gridGap: '15px' }}>
        <Typography variant="h5">Basic Signup Validations</Typography>
        {Object.keys(field).map(key => (
          <FormField key={key} field={form.getField(key)} />
        ))}
        <Button onClick={() => console.log(form.getValues())} disabled={form.hasError} variant="contained">
          Save Changes
        </Button>
        <Typography>Has Changes: {`${form.hasChanges}`}</Typography>
        <Typography>Has hasError: {`${form.hasError}`}</Typography>
        <Button onClick={() => form.showErrors()} variant="contained">
          Show Errors
        </Button>
      </Paper>
    </Box>
  );
};

export default BasicExample;

Live working Examples

https://codesandbox.io/examples/package/@surinderlohat/react-form-validation

Like this package ? show some love by start this repo

For more packages like this please checkout: https://github.com/surinderlohat

License

MIT Free Software!

FAQs

Last updated on 03 Jun 2022

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc