# react-form-validator-core

> Core validator component for react forms.

Latest version **2.0.2** (published 2025-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-form-validator-core
pnpm add react-form-validator-core
yarn add react-form-validator-core
bun add react-form-validator-core
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2025-02-24 |
| First published | 2017-09-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 36.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 95 |
| Author | NewOldMax |
| Maintainers | newoldmax |
| Keywords | react, form, validation |

## Links

- npm: https://www.npmjs.com/package/react-form-validator-core
- Repository: https://github.com/NewOldMax/react-form-validator-core
- Homepage: https://github.com/NewOldMax/react-form-validator-core#readme
- Issues: https://github.com/NewOldMax/react-form-validator-core/issues
- npm.io page: https://npm.io/package/react-form-validator-core

## Dependencies (3)

- [prop-types](https://npm.io/package/prop-types.md) ^15.0.0
- [promise-polyfill](https://npm.io/package/promise-polyfill.md) 8.1.0
- [react-lifecycles-compat](https://npm.io/package/react-lifecycles-compat.md) ^3.0.2

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2025-02-24
- 2.0.1 — 2024-10-23
- 2.0.0 — 2024-10-23
- 1.1.2 — 2022-06-07
- 1.1.1 — 2021-02-04
- 1.1.0 — 2021-02-03
- 1.0.1 — 2021-01-22
- 1.0.0 — 2020-07-28
- 0.7.0 — 2020-06-16
- 0.6.5 — 2020-01-29
- 0.6.4 — 2019-06-10
- 0.6.3 — 2019-04-18
- 0.6.2 — 2019-02-28
- 0.6.1 — 2019-02-19
- 0.6.0 — 2019-02-19
- … 13 more at https://npm.io/package/react-form-validator-core/versions

## README

## Validation component for react forms

[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://opensource.org/licenses/MIT)
[![npm version](https://badge.fury.io/js/react-form-validator-core.svg)](https://badge.fury.io/js/react-form-validator-core)
[![Build Status](https://travis-ci.org/NewOldMax/react-form-validator-core.svg?branch=master)](https://travis-ci.org/NewOldMax/react-form-validator-core)

Simple form validation component for react forms inspired by [formsy-react](https://github.com/christianalfoni/formsy-react)

Default validation rules:
+ matchRegexp
+ isEmail
+ isEmpty
+ required
+ trim
+ isNumber
+ isFloat
+ isPositive
+ minNumber
+ maxNumber
+ minFloat
+ maxFloat
+ isString
+ minStringLength
+ maxStringLength
+ maxFileSize
+ allowedExtensions

Some rules can accept extra parameter, example:
````javascript
<YourValidationInput
   {...someProps}
   validators={['minNumber:0', 'maxNumber:255', 'matchRegexp:^[0-9]$']}
/>
````

### Usage

````javascript
import React from 'react';
import { ValidatorComponent } from 'react-form-validator-core';

class TextValidator extends ValidatorComponent {

    render() {
        const { errorMessages, validators, requiredError, validatorListener, ...rest } = this.props;

        return (
            <div>
                <input
                    {...rest}
                    ref={(r) => { this.input = r; }}
                />
                {this.errorText()}
            </div>
        );
    }

    errorText() {
        const { isValid } = this.state;

        if (isValid) {
            return null;
        }

        return (
            <div style={{ color: 'red' }}>
                {this.getErrorMessage()}
            </div>
        );
    }
}

export default TextValidator;
````

````javascript
...
import { ValidatorForm } from 'react-form-validator-core';
...
render() {
    return (
        <ValidatorForm
            ref="form"
            onSubmit={this.handleSubmit}
        >
            <TextValidator
                onChange={this.handleChange}
                name="email"
                value={email}
                validators={['required', 'isEmail']}
                errorMessages={['this field is required', 'email is not valid']}
            />
            <button type="submit">submit</button>
        </ValidatorForm>
    );
}
...
````

````javascript

class FileValidator extends ValidatorComponent {
  render() {
    const { errorMessages, validators, requiredError, validatorListener, value, ...rest } = this.props;
    return (
      <div>
        <input type="file" {...rest}>
        {this.errorText()}
      </div>
    );
  }

  errorText() {
    const { isValid } = this.state;

    if (isValid) {
      return null;
    }

    return <div style={{ color: "red" }}>{this.getErrorMessage()}</div>;
  }
}
export default FileValidator;

...
import { ValidatorForm } from 'react-form-validator-core';
...
render() {
    return (
        <ValidatorForm
            ref="form"
            onSubmit={this.handleSubmit}
        >
            <FileValidator
                onChange={this.handleChange}
                name="file"
                type="file"
                value={file}
                validators={['isFile', 'maxFileSize:' + 1 * 1024 * 1024, 'allowedExtensions:image/png,image/jpeg']}
                errorMessages={['File is not valid', 'Size must not exceed 1MB', 'Only png and jpeg']}
            />
            <button type="submit">submit</button>
        </ValidatorForm>
    );
}
...
````

#### You can add your own rules
````javascript
ValidatorForm.addValidationRule('isPasswordMatch', (value) => {
    if (value !== this.state.user.password) {
        return false;
    }
    return true;
});
````
Get them
````javascript
ValidatorForm.getValidationRule('isPasswordMatch');
````
Remove them
````javascript
ValidatorForm.removeValidationRule('isPasswordMatch');
````
And check is validation rule already in list
````javascript
ValidatorForm.hasValidationRule('isPasswordMatch');
````

### Migration guide

#### From 0.x to 1.x

Breaking changes was introduced in order to avoid legacy context. You should change `render` method of your input components to `renderValidatorComponent`.

Before:
````javascript
import React from 'react';
import { ValidatorComponent } from 'react-form-validator-core';

class TextValidator extends ValidatorComponent {
    render() {
        // return your validated component
    }
}

export default TextValidator;
````

After:
````javascript
import React from 'react';
import { ValidatorComponent } from 'react-form-validator-core';

class TextValidator extends ValidatorComponent {
    renderValidatorComponent() {
        // return your validated component
    }
}

export default TextValidator;
````

### API

#### ValidatorForm

+ Props

| Prop            | Required | Type     | Default value | Description                                                                                                                  |
|-----------------|----------|----------|---------------|------------------------------------------------------------------------------------------------------------------------------|
| onSubmit        | true     | function |               | Callback for form that fires when all validations are passed                                                                 |
| instantValidate | false    | bool     | true          | If true, form will be validated after each field change. If false, form will be validated only after clicking submit button. |
| onError         | false    | function |               | Callback for form that fires when some of validations are not passed. It will return array of elements which not valid. |
| debounceTime    | false    | number   | 0             | Debounce time for validation i.e. your validation will run after `debounceTime` ms when you stop changing your input |

+ Instance methods (via ref)

| Name             | Params | Return | Description                                        |
|------------------|--------|--------|----------------------------------------------------|
| resetValidations |        |        | Reset validation messages for all validated inputs |
| isFormValid      | dryRun: bool (default true) | Promise   | Get form validation state in a Promise (`true` if whole form is valid). Run with `dryRun = false` to show validation errors on form |

+ Static methods (via class)

| Name             | Params | Return | Description                                        |
|------------------|--------|--------|----------------------------------------------------|
| addValidationRule | name: string, callback: function |        | Add new validation rule |
| getValidationRule | name: string | function | Get validation rule by name |
| hasValidationRule | name: string | bool     | Check if rule exsits  |
| removeValidationRule | name: string |       | Remove validation rule  |

#### All validated fields (ValidatorComponent)

+ Props

| Prop            | Required | Type     | Default value | Description                                                                            |
|-----------------|----------|----------|---------------|----------------------------------------------------------------------------------------|
| validators      | false    | array    |               | Array of validators. See list of default validators above.                             |
| errorMessages   | false    | array    |               | Array of error messages. Order of messages should be the same as `validators` prop.    |
| name            | true     | string   |               | Name of input                                                                          |
| validatorListener | false  | function |               | It triggers after each validation. It will return `true` or `false`                    |
| withRequiredValidator | false | bool  |               | Allow to use `required` validator in any validation trigger, not only form submit      |
| containerProps | false | object  |               | Allow to customize input wrapper `div`      |

+ Methods

| Name             | Params | Return | Description                                        |
|------------------|--------|--------|----------------------------------------------------|
| getErrorMessage  |        |        | Get error validation message                       |
| validate         | value: any, includeRequired: bool | | Run validation for current component |
| isValid          |        | bool   | Return current validation state                    |
| makeInvalid      |        |        | Set invalid validation state                       |
| makeValid        |        |        | Set valid validation state                         |

### Implementations

 + [material-ui](https://www.npmjs.com/package/react-material-ui-form-validator)

### Contributing

This component covers all my needs, but feel free to contribute.

---
_Source: https://npm.io/package/react-form-validator-core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
