# @react-forms/validations

> react-forms validations add-on

Latest version **0.1.0** (published 2019-06-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install @react-forms/validations
pnpm add @react-forms/validations
yarn add @react-forms/validations
bun add @react-forms/validations
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2019-06-24 |
| First published | 2019-06-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Trey Hakanson |
| Maintainers | trey.hakanson |

## Links

- npm: https://www.npmjs.com/package/@react-forms/validations
- Repository: https://github.com/treyhakanson/react-forms
- npm.io page: https://npm.io/package/@react-forms/validations

## Dependencies (2)

- [is-url](https://npm.io/package/is-url.md) ^1.2.4
- [is-email](https://npm.io/package/is-email.md) ^1.0.0

## Recent versions

- 0.1.0 (latest) — 2019-06-24

## README

# React Forms

[![npm version](https://badge.fury.io/js/%40react-forms%2Fcore.svg)](https://badge.fury.io/js/%40react-forms%2Fcore)

## Installation

To install:

```sh
yarn add @react-forms/core
# or
npm install --save @react-forms/core
```

## Overview

There's a lot of other form libraries out there. Most of them are bloated, unintutive, and overcomplicated. Some of them are even built around violating core design principles of the frameworks they work with (looking at you `redux-form`!). Forms used to be easy, and `react-forms` seeks to bring things back. React forms has:

- 🚀 Zero dependencies in the core
- 🤤 Dead simple API
- ⚡️ Lightning fast execution
- 🔧 Maximum extensibility

`react-forms` was built with good form design in mind, and will help you build better, more consistent forms in a fraction of the time.

## Documentaion

Component documentation and interactive examples can be found [here.](https://treyhakanson.github.io/react-forms)

### Add-Ons

- `@react-forms/validations`: some simple field validations to get you started

### A Simple Form

Creating a form with `react-forms` is dead simple:

```js
import React from "react";
import { Form, Field } from "@react-froms/core";
import { isRequired, isEmail } from "@react-forms/validations";

class MyCoolForm extends React.Component {
  onSubmit(event, shouldSubmit, errors, values) {
    if (shouldSubmit) {
      alert("Looks good to me!");
    }
  }

  render() {
    <Form
      fields={{
        "first-name": {
          validations: [isRequired]
        },
        "last-name": {
          validations: [isRequired]
        },
        email: {
          validations: [isRequired, isEmail]
        }
      }}
      formProps={{ action: "/", method: "post" }}
      onSubmit={this.onSubmit}
    >
      <Field.Input name="first-name" label="First Name" />
      <Field.Input name="last-name" label="Last Name" />
      <Field.Input name="email" label="Email" />
      <Field.Submit text="Submit" />
    </Form>;
  }
}
```

### Custom Fields

For convenience, `react-forms` provides a built-in `Field` component that is easily stylized and suitable for most use cases. However, there may be scenarios where it just doesn't cut it. Luckily, creating a custom field is easy! `react-forms` leverages React's new context API, so all you need to do is have your custom field subscribe to it. A helper has been provided for convenience:

```js
import * as React from "react";
import { withForm } from "@react-forms/core";

class MyCoolField extends React.Component {
  render() {
    const { errors, inputProps } = this.props;
    console.error(errors);
    return <input {...inputProps} />;
  }
}

export default withForm(MyCoolField);
```

See, easy! its **very important** to note that in the above example, the input element is given all attributes of `this.props.inputProps`. These props contain things like on change handlers and such that `react-forms` needs on its fields to function properly. Overriding attributes from `inputProps` is easy too though:

```js
import * as React from "react";
import { withForm } from "@react-forms/core";

class MyCoolField extends React.Component {
  _myCoolOnChangeMethod = value => {
    const { onChange } = this.props.inputProps;
    console.log("Look at me doing extra stuff 😎");
    onChange(value);
  };

  render() {
    const { inputProps, errors } = this.props;
    console.error(errors);
    return <input {...inputProps} onChange={this._myCoolOnChangeMethod} />;
  }
}

withForm(MyCoolField);
```

### Styling

`react-forms` is meant to handle your forms' logic, not their styling. However, `react-forms` maintains logical css class names so you can quickly style things using your favorite tools. The general structure of `react-forms` `Field` components are as follows:

```html
<div class="Field [Field--invalid]">
  <label className="Field__Label"></label>
  <input className="Field__Input" />
  <ul className="Field__ErrorList">
    <li className="Field__ErrorList__Error"></li>
  </ul>
</div>
```

`react-forms` also passes down css classnames, so you can CSS-in-JS libraries like `styled-components` to extend them. Here's an example of applying some quick styling:

```js
const MyCustomField = styled(Field)`
  // give the input a red border when invalid
  .Field--invalid .Field__Input {
    border: 1px solid red;
  }
`;
```

## Developing Locally

If you'd like to build and develop this project locally, just pull down the repo, build the libraries, and run the sandbox:

```sh
# Build everything
yarn build

# Watch packages being developed
yarn core:dev

# Start the sandbpx
yarn sandbox:dev
```

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