• Stars
    star
    2,595
  • Rank 17,663 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A form input builder and validator for React JS

Moved!

This project has moved. Starting from 1.0.0 onward, develeopment will continue at https://github.com/formsy/formsy-react/


formsy-react GitHub release Build Status

A form input builder and validator for React JS

How to use API Examples

Background

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

The main concept is that forms, inputs and validation is done very differently across developers and projects. This extension to React JS 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. Ex. "onSubmit", "onError", "onValid" etc.

  4. Pass external errors to the form to invalidate elements

  5. You can dynamically add form elements to your form and they will register/unregister to the form

Default elements

You can look at examples in this repo or use the formsy-react-components project to use bootstrap with formsy-react, or use formsy-material-ui to use Material-UI with formsy-react.

Install

  1. Download from this REPO and use globally (Formsy) or with requirejs
  2. Install with npm install formsy-react and use with browserify etc.
  3. Install with bower install formsy-react

Changes

Check out releases

Older changes

How to use

See examples folder for examples. Codepen demo.

Complete API reference is available here.

Formsy gives you a form straight out of the box

  import Formsy from 'formsy-react';

  const MyAppForm = React.createClass({
    getInitialState() {
      return {
        canSubmit: false
      }
    },
    enableButton() {
      this.setState({
        canSubmit: true
      });
    },
    disableButton() {
      this.setState({
        canSubmit: false
      });
    },
    submit(model) {
      someDep.saveEmail(model.email);
    },
    render() {
      return (
        <Formsy.Form onValidSubmit={this.submit} onValid={this.enableButton} onInvalid={this.disableButton}>
          <MyOwnInput name="email" validations="isEmail" validationError="This is not a valid email" required/>
          <button type="submit" disabled={!this.state.canSubmit}>Submit</button>
        </Formsy.Form>
      );
    }
  });

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

Building a form element (required)

  import Formsy from 'formsy-react';

  const MyOwnInput = React.createClass({

    // Add the Formsy Mixin
    mixins: [Formsy.Mixin],

    // setValue() will set the value of the component, which in
    // turn will validate it and the rest of the form
    changeValue(event) {
      this.setValue(event.currentTarget.value);
    },

    render() {
      // Set a specific className based on the validation
      // state of this component. showRequired() is true
      // when the value is empty and the required prop is
      // passed to the input. showError() is true when the
      // value typed is invalid
      const className = this.showRequired() ? 'required' : this.showError() ? 'error' : null;

      // An error message is returned ONLY if the component is invalid
      // or the server has returned an error message
      const errorMessage = this.getErrorMessage();

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

The form element component is what gives the form validation functionality to whatever you want to put inside this wrapper. You do not have to use traditional inputs, it can be anything you want and the value of the form element can also be anything you want. As you can see it is very flexible, you just have a small API to help you identify the state of the component and set its value.

Related projects

Contribute

  • Fork repo
  • npm install
  • npm run examples runs the development server on localhost:8080
  • npm test runs the tests

License

The MIT License (MIT)

Copyright (c) 2014-2016 PatientSky A/S

More Repositories

1

webpack-express-boilerplate

A boilerplate for running a Webpack workflow in Node express
JavaScript
1,398
star
2

webpack-bin

A webpack code sandbox
JavaScript
710
star
3

flux-angular

Use the FLUX architecture with Angular JS
JavaScript
313
star
4

react-app-boilerplate

React application boilerplate
JavaScript
305
star
5

EmptyBox

A complete isomorphic hackable blog service based on React JS
JavaScript
211
star
6

react-webpack-cookbook

Temp repo for transfer
HTML
187
star
7

flux-react-boilerplate

A boilerplate for a full FLUX architecture
JavaScript
166
star
8

react-states

Explicit states for predictable user experiences
TypeScript
139
star
9

flux-react

A library combining tools to develop with a FLUX architecture
JavaScript
139
star
10

markdown-to-react-components

Convert markdown into react components
JavaScript
132
star
11

rxjs-react-component

A component allowing you to change state using observables
JavaScript
119
star
12

create-ssl-certificate

Command line tool to create self signed SSL certificate
JavaScript
97
star
13

immutable-store

An easy to use immutable store for React JS and FLUX architecture
JavaScript
90
star
14

impact

Reactive state management for React
TypeScript
86
star
15

webpack-example

An example of webpack workflow
JavaScript
78
star
16

proxy-state-tree

An implementation of the Mobx/Vue state tracking approach, for library authors
JavaScript
66
star
17

timsy

Agnostic functional state machine with epic type support
TypeScript
58
star
18

redux-nodes

Nodes instead of reducers
TypeScript
47
star
19

react-simple-flex

An intuitive abstraction over flexbox
JavaScript
47
star
20

reactive-router

A reactive wrapper around Page JS
JavaScript
46
star
21

react-addressbar

Take control of the addressbar in a reactive way
JavaScript
42
star
22

form-data-to-object

Converts application/x-www-form-urlencoded keys to plain JS object
JavaScript
36
star
23

react-packaging

Examples of how to create a workflow with React JS using different packaging tools
JavaScript
31
star
24

jflux

An easy to use unidirectional component based framework
JavaScript
29
star
25

objectory

Object factory honoring JavaScripts prototype delegation
JavaScript
27
star
26

npm-extractor

Extracts npm packages into memory
JavaScript
18
star
27

emmis

Create a chaining API for your application
TypeScript
17
star
28

isomorphic-react-baobab-example

An example related to article about using baobab to create an isomorphic react app
JavaScript
17
star
29

process-control

A tool for managing continuous async process by starting, stopping, restarting and disposing
TypeScript
17
star
30

flutter_observable_state

Observable state for flutter applications
Dart
16
star
31

angular-flux

A plugin for Angular JS that lets you write code with the FLUX pattern
JavaScript
16
star
32

exploring-elm-boilerplate

A boilerplate used in an article series exploring Elm
JavaScript
14
star
33

baobab-hot-loader

Using Webpack hot replacement update your application state without reload
JavaScript
13
star
34

predictable-user-experiences-in-react-book

A book about predictable user experiences in React
TypeScript
13
star
35

cerebral-react-baobab

A Cerebral package with React and Baobab
JavaScript
13
star
36

op-op-spec

The op-op specification. A simple straight forward approach to functional programming
13
star
37

flux-react-router

A router in the flux-react family
JavaScript
13
star
38

signalit

Manage state locally and globally in React with signals
TypeScript
13
star
39

observable-state

Observables for creating reactive functional state in applications
JavaScript
11
star
40

cerebral-react-immutable-store

Cerebral package with react and immutable store
JavaScript
10
star
41

R

An application framework using functional reactive concepts and virtual-dom
JavaScript
9
star
42

flux-react-dispatcher

React dispatcher for the FLUX architecture
JavaScript
9
star
43

backbone-draganddrop-delegation

A consistent drag and drop across browsers using native like events
JavaScript
9
star
44

react-environment-interface

Define and consume a custom environment for your application
TypeScript
8
star
45

baobab-angular

A state handling library for Angular, based on Baobab
JavaScript
8
star
46

react-nodesy

React node hierarchy with render props
TypeScript
7
star
47

chrome-recorder

Lets you easily record audio and video in Chrome
JavaScript
7
star
48

cerebral-angular-immutable-store

A cerebral package for angular and immutable-store
JavaScript
7
star
49

reactive-app

Build large scale applications supported by visual tools
TypeScript
5
star
50

virtual-dom-loader

A virtual-hyperscript, used in virtual-dom, to JSX converter for Webpack
JavaScript
5
star
51

HotDiggeDy

Test project for using observables to build complex apps
JavaScript
5
star
52

typed-client-router

TypeScript
4
star
53

batchcalls

Batches calls to a function and passes arguments as arrays
JavaScript
4
star
54

cerebral-boilerplate

A webpack boilerplate for cerebral applications
JavaScript
4
star
55

class-states

Manage async complexity with states
TypeScript
4
star
56

cerebral-router-demo

The demo code for the cerebral router introduction
JavaScript
4
star
57

actorial

Actor pattern with state machines
TypeScript
4
star
58

flux-react-store

A simple construct for your flux stores
JavaScript
4
star
59

create-gql-api

Simplify GQL typing and consumption
JavaScript
4
star
60

christianalfoni.com

My personal website
TypeScript
4
star
61

redux-proxy-thunk

TypeScript
3
star
62

TeachKidsCode

An open source service for learning code
JavaScript
3
star
63

formsy-angular

A form input builder and validator for Angular JS
JavaScript
3
star
64

the-angular-experiment

An experiment bringing components and immutable state to Angular
JavaScript
3
star
65

grunt-tdd

Run browser and Node JS tests on Buster, Mocha or Jasmine and get the reports in the browser
JavaScript
3
star
66

boilproject-service

The service that allows you to create boilerplates
TypeScript
2
star
67

new-git-flow

Created with CodeSandbox
HTML
2
star
68

cerebral-immutable-store

Immutable Store Model layer for Cerebral
JavaScript
2
star
69

family-scrum-v2

TypeScript
2
star
70

webpack-express-isomorphic-react-boilerplate

An isomorphic boilerplate with react, babel, routing and express on the server
2
star
71

middleend-boilerplate

A boilerplate for building applications with a middleend
2
star
72

markdown-excalidraw

TypeScript
2
star
73

codesandbox-weekplanner

Created with CodeSandbox
TypeScript
2
star
74

lib-boilerplate

A boilerplate for creating any JavaScript frontend, or frontend+backend, library
JavaScript
2
star
75

models-test-2

Created with CodeSandbox
JavaScript
1
star
76

grunt-buster-tdd

A browser and Node TDD tool using Buster JS
JavaScript
1
star
77

devchat

TypeScript
1
star
78

C

An inheritance experiment which allows for private variables and "super" up the constructor chain
JavaScript
1
star
79

boilproject-cli

An NPX tool to easily start a new project
JavaScript
1
star
80

Protos

Inheritance the native JavaScript way
JavaScript
1
star
81

our-project-design-system

Created with CodeSandbox
JavaScript
1
star
82

hm

1
star
83

create-css-theme

Converts your theme into css variables for easy consumption with CSS approach
1
star
84

contribute-vscode-plugin

Contribute to github for beginners
TypeScript
1
star
85

module-loader-tdd

An easy to use and easy to test module loader
JavaScript
1
star
86

validate

A general JavaScript validation tool with pre-configuration, Backbone support and can be used cross client/server
1
star