Go to file
Aesop Wolf 42e39cbc8f Chore/tweaks for v1 (#473)
* Rename HOC/Wrapper export to `withFormsy`

This is more in alignment with community practices.

* Migrate from npm to yarn

* Update package.json

Use order listed on https://yarnpkg.com/en/docs/package-json

* Update README.md

New examples, and small copy/whitespace changes

* Remove CHANGES.md

We will use github release from now on

* Fix test suite

Replace all references to  with

* Add ESLint

* Update dependencies

* Upgrade babel dependencies

* Upgrade jsdom

* Upgrade nodeunit and sinon

* Upgrade webpack and webpack-dev-server

* Fix examples

* Convert to ES6 classes

* Fix ESLint errors and warnings (WIP)

* Fix more ESLint errors and warnings (WIP)

* Move runRules to utils.js

* Fix more ESLint errors and warnings (WIP)

* Fix more ESLint errors and warnings (WIP)

* Fix more ESLint errors and warnings (WIP)

* Use less complex regex's for url and email

* Change grammar in README

* Change export pattern

* Use ES6 export for utils and validationRules

* Fix login example

* Reorder methods alphabetically and remove "magical" `validate` feature

* Remove `validate` from API docs (and general cleanup of file)

* Update examples (removes `validate` feature)

* Rename webpack file and remove json loader

* Fix code samples in README

* Update reset-values example (WIP)

* Cleanup reset-values example

* Fix prop type for Wrapper value

* Handle onReset event

* Update reset-value example to support `<button type="reset">`

* Update dynamic form fields example
2017-08-29 16:35:17 -07:00
examples Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
release Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
src Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
tests Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
.babelrc Update dependencies. Some minor changes. 2016-04-19 11:12:14 +03:00
.editorconfig Clean up 2015-04-22 19:06:53 +03:00
.eslintrc Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
.gitignore Update dependencies. Some minor changes. 2016-04-19 11:12:14 +03:00
.npmignore Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
.nvmrc Major update/cleanup to Formsy API; React v15+; No more mixins (#470) 2017-08-08 22:09:11 -07:00
.travis.yml Major update/cleanup to Formsy API; React v15+; No more mixins (#470) 2017-08-08 22:09:11 -07:00
API.md Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
LICENSE Update LICENSE 2016-10-05 14:46:46 +02:00
README.md Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
package.json Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
testrunner.js Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
webpack.config.js Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00
yarn.lock Chore/tweaks for v1 (#473) 2017-08-29 16:35:17 -07:00

README.md

formsy-react GitHub release Build Status

A form input builder and validator for React.

Quick Start API Examples

Background

I wrote an article on forms and validation with React, Nailing that validation with React JS, the result of that was this component.

The main concept is that forms, inputs, and validation are done very differently across developers and projects. This React component aims to be that “sweet spot” between flexibility and reusability.

What You Can Do

  1. Build any kind of form element components. Not just traditional inputs, but anything you want, and get that validation for free
  2. Add validation rules and use them with simple syntax
  3. Use handlers for different states of your form. (onError, onSubmit, onValid, etc.)
  4. Pass external errors to the form to invalidate elements (E.g. a response from a server)
  5. Dynamically add form elements to your form and they will register/unregister to the form

Install

yarn add formsy-react react react-dom and use with webpack, browserify, etc.

Quick Start

1. Build a Formsy element

// MyInput.js
import { withFormsy } from 'formsy-react';
import React from 'react';

class MyInput extends React.Component {
  constructor(props) {
    super(props);
    this.changeValue = this.changeValue.bind(this);
  }

  changeValue(event) {
    // setValue() will set the value of the component, which in
    // turn will validate it and the rest of the form
    // Important: Don't skip this step. This pattern is required
    // for Formsy to work.
    this.props.setValue(event.currentTarget.value);
  }

  render() {
    // An error message is returned only if the component is invalid
    const errorMessage = this.props.getErrorMessage();

    return (
      <div>
        <input
          onChange={this.changeValue}
          type="text"
          value={this.props.getValue() || ''}
        />
        <span>{errorMessage}</span>
      </div>
    );
  }
}

export default withFormsy(MyInput);

withFormsy is a Higher-Order Component that exposes additional props to MyInput. See the API documentation to view a complete list of the props.

2. Use your Formsy element

import Formsy from 'formsy-react';
import React from 'react';
import MyInput from './MyInput';

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.disableButton = this.disableButton.bind(this);
    this.enableButton = this.enableButton.bind(this);
    this.state = { canSubmit: false };
  }

  disableButton() {
    this.setState({ canSubmit: false });
  }

  enableButton() {
    this.setState({ canSubmit: true });
  }

  submit(model) {
    fetch('http://example.com/', {
      method: 'post',
      body: JSON.stringify(model)
    });
  }

  render() {
    return (
      <Formsy onValidSubmit={this.submit} onValid={this.enableButton} onInvalid={this.disableButton}>
        <MyInput
          name="email"
          validations="isEmail"
          validationError="This is not a valid email"
          required
        />
        <button type="submit" disabled={!this.state.canSubmit}>Submit</button>
      </Formsy>
    );
  }
}

This code results in a form with a submit button that will run the submit method when the form is submitted with a valid email. The submit button is disabled as long as the input is empty (required) and the value is not an email (isEmail). On validation error it will show the message: "This is not a valid email".

Contribute

  • Fork repo
  • yarn
  • yarn examples runs the development server on localhost:8080
  • yarn test runs the tests

Changelog

Check out releases

License

The MIT License (MIT)

Copyright (c) 2014-2016 PatientSky A/S