Skip to content

Validators to use with react-final-form and redux-form

Notifications You must be signed in to change notification settings

Dema/final-form-validations

Repository files navigation

final-form-validations

set of validators to use with final-form or redux-form

NPM JavaScript Style Guide

Install

yarn add final-form-validations

Usage

import React, { Component } from "react";
import { Form, Field } from "rect-final-form";

import {
  createValidator,
  composeFieldValidators,
  required,
  minLength,
  maxLength,
  lessOrEqual,
} from "@dmitry.olyenyov/final-form-validations";

const validators = createValidator({
  lastName: [
    requried("Required!"),
    minLength(2, "Please enter at least 2 chars"),
    maxLength(500, "Name is too long"),
  ],
  description: required("Required!"),
});

class Example extends Component {
  render() {
    return (
      <Form onSubmit={() => {}} validate={validators}>
        <Field name="lastName" component="input" />
        <Field name="description" component="input" />
        <Field
          name="someField"
          component="input"
          validate={composeFieldValidators(
            required("Required!"),
            lessOrEqual(1000, "Must be less than 1000")
          )}
        />
      </Form>
    );
  }
}

License

MIT © Dema

About

Validators to use with react-final-form and redux-form

Resources

Stars

Watchers

Forks

Packages

No packages published