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?
Just for the record, you can just call props. reset() on the component which has the reduxForm HOC. No extra line of code required.
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.
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.
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.
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'
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With