Formsy react validation ( server response )

718 Views Asked by At

I'm using formsy-react ( for client validation ). It works perfect for now, but i have to pass error if something bad on server side.

Is it possible to take response when form was submitted? Get response from server?

Or how i can achieve that without formsy ? I'm new in react Thanks for help. Appreciate it !

1

There are 1 best solutions below

0
Minh Ha Pham On

It is very easy task with Formsy-react. You could use updateInputWithError method (Documents here) or use invalidateForm which is passed as a parameter of onValidSubmit.

Example with onValidSubmit :

const App = React.createClass({
  ...
  submit(model, resetForm, invalidateForm) {
    this.callServerAPI()
        .then(...)
        .catch((serverErrors) => {
           // serverErrors should be 
           // { Name: 'Name is too long', Class: 'Class is not valid' }
           invalidateForm(serverErrors); // Just call invalidateForm
        });
  },
  render() {
    return (
      <Formsy.Form ref="form" onValidSubmit={this.submit}>
        <MyInput name="Name" title="Name" value={user.name} />
        <MyInput name="Class" title="Class" value={user.class} />
        <div className="buttons">
          <button type="submit">Submit</button>
        </div>
      </Formsy.Form>
    );
  }
});