Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually set redux-form field and/or form errors

I'm aware that if you throw a SubmissionError from your handleSubmit() function, the redux-form code will fill in the errors of the appropriate fields and/or the form itself.

Yet that API of setting field/form errors, tightly couples our implementation of handleSumbit() to be a caller of the redux-form code (which contains the SubmissionError exception handler).

My use case is to have something like so:

function asyncActionDispatcher(values) {                                     
  return (dispatch, getState) => {                                           
    // I'm using getState, which is not accessible in handleSubmit()         
    // But I'd also like to be able to set errors on the form fields and/or the
    // form.                                                                 
  };                                                                         
}                                                                            

function handleSubmit(values, dispatch) {                                    
  dispatch(                                                                  
    asyncActionDispatcher(values)                                            
  );                                                                         
} 

I can't throw a SubmissionError in asyncActionDispatcher() because it's called by redux and not redux-form.

Does redux-form have another API to set errors on fields/form?

like image 843
pleasedesktop Avatar asked Feb 07 '17 08:02

pleasedesktop


People also ask

How do you reset the form after submit in Redux form?

Just for the record, you can just call props. reset() on the component which has the reduxForm HOC. No extra line of code required.

What is FieldArray in Redux form?

The FieldArray component is how you render an array of fields. It works a lot like Field . With Field , you give a name , referring to the location of the field in the Redux state, and a component to render the field, which is given the props to connect the field to the Redux state.

How does Redux handle form?

Redux Form is very easy to use and implement, we just have to wrap our form component with the HOC provided by Redux Form and we are good to go. Applying validation to the form is very easy in Redux Form, we can apply validation to all the fields as well as validations for individual fields.

What is submitting in Redux form?

There are two ways to give redux-form a function to run when your form is submitted: Pass it as an onSubmit prop to your decorated component. In which case, you would use onSubmit={this. props. handleSubmit} inside your decorated component to cause it to fire when the submit button is clicked.


1 Answers

Does redux-form have another API to set errors on fields/form?

Another option here when asyncValidate and other options proposed won't work (for example because validations consider multiple forms) is to dispatch updateSyncErrors directly. Example usage below:

const { updateSyncErrors } = require('redux-form/lib/actions').default;

dispatch(updateSyncErrors('formName', {
  fieldName: 'Some Error Message'
}));
like image 57
quetzaluz Avatar answered Sep 21 '22 20:09

quetzaluz