1

So i am using redux-form and following the example for async validation in documentation, everything is working fine but i want to avoid using throw to throw object like below

throw {email:"Already exists"}

I have tried using "SubmissionError" but when i throw SubmissionError, it adds and object called "errors" inside "asyncErrors" in redux and then errors are not displayed below field, they are displyed if i just throw it like this throw {email:"Already exists"} Below is my code using SubmissionError

import { SubmissionError } from 'redux-form/immutable';
import { doesUserExist } from '../../shared/endpoints/userEndpoint';

const asyncValidate = (values /*, dispatch */) => {
  return doesUserExist(values.get('email')).then((res) => {
    if (res.registered) {
      throw new SubmissionError({ email: 'This email is already in use.' });
    }
  });
};

export default asyncValidate;

output redux state is -

form: {
registerForm:{
 asyncErrors:{
  errors:{email: "This email is already in use."}
  }
 }
}
4
  • Can you include the actual output that you are getting after throwing SubmissionError? Commented Nov 18, 2020 at 16:07
  • i have added the output Commented Nov 18, 2020 at 16:12
  • Are you getting this output after you throw SubmissionError? Where is the string Already Exists in your code? Commented Nov 18, 2020 at 16:15
  • sorry, i have changed the output Commented Nov 18, 2020 at 16:24

1 Answer 1

2

The SubmissionError in redux forms will add general errors in asyncErrors object as it is given in the docs.

If it is rejected with a redux-form SubmissionError containing errors in the form { field1: 'error', field2: 'error' } then the submission errors will be added to each field (to the error prop) just like async validation errors are.

Refer here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.