FormHandler.md

January 12, 2016 ยท View on GitHub

Using the FormHandlerMixin

React Form Builder provide a FormHandlerMixin you can use to instantly give your components some basic form behavior and persistence.

It adds some props that are used to set default settings or can be overidden. They are all reflected to the state of that component on getInitialState.

nametypedescrition
titlestringthe form title. default: ""
submittingbooleanis the form being submitted. default: false
serverErrorsobjectlets render errors that were not catched by Validators. default: null
formDataobjectthe serialized representation of the form data. default: {}
formDefobjectsee Form Definitions
buttonsobjectsee Customizing Buttons

There are two requirements :

  • Specify a Form Definition through the formDef, either by setting it in getDefaultProps or by passing it from a parent component.
  • implement a onSubmit function that will be called when trying to submit the form. Example:
onSubmit: function(callback) {
     console.log(this.state.formData); // This will generally be a remote call instead
     callback();
}

Full example :

var React = require('react');
var ReactDOM = require('react-dom');
var FormHandler = require('react-form-builder').Mixins.Handler;
var ExampleFormDef = {
  type: 'form',
  components: [
    {
      type: 'text-field',
      dataKey: 'example',
      displayName: 'Example'
    }
  ]
};

var Example = React.createClass({
  mixins: [FormHandler],

  getDefaultProps: function() {
    return {
      formDef: ExampleFormDef
    };
  },

  onSubmit: function(callback) {
    console.log("FormData: ", this.state.formData);
    callback();
  },

  render: this.renderForm();
});

ReactDOM.render(Example(), document.body);