gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

Add context (tcomb-validation v2.2.0) support to tcomb-form #197

Closed gcanti closed 9 years ago

gcanti commented 9 years ago

The plan:

v0.7.1

Example

const Name = t.subtype(t.String, (s) => s.length > 2);

Name.getValidationErrorMessage = function (value, path, context) {
  return 'error message with locale: ' + context.locale;
};

const Schema = t.struct({
  name: Name
});

const options = {
  fields: {
    name: {
      // error: Name.getValidationErrorMessage // <= no more required
    }
  }
};

const App = React.createClass({

  onSubmit(evt) {
    evt.preventDefault();
    var value = this.refs.form.getValue();
    if (value) {
      console.log(value);
    }
  },

  render() {
    return (
      <form onSubmit={this.onSubmit}>
        <t.form.Form
          ref="form"
          type={Schema}
          options={options}
          context={{locale: 'it-IT'}} // <= new prop
        />
        <button type="submit" className="btn btn-primary">Save</button>
      </form>
    );
  }

});
th0r commented 9 years ago

Great! You rock!

gcanti commented 9 years ago

Released https://github.com/gcanti/tcomb-form/releases/tag/v0.7.1