1

I'm trying to validate the username and email with my express-app, but on the asyncValidate method, when i validate one, the other error state disappear.

const asyncValidate = (values, dispatch, props, field) => {
    if (field === 'username') {
        const url = config.dev_api_url + `/validation/username/${values.username}`
        return axios.get(url).then(res => {
            if (res.data.username === true) {
                throw { username: 'Already exists' }
            }
        })
    } else if (field === 'email') {
        const url = config.dev_api_url + `/validation/email/${values.email}`
        return axios.get(url).then(res => {
            if (res.data.email === true) {
                throw { email: 'Already exists' }
            }
        })
    }
}

Here is the function where i render the input error.

renderInput = form => {
    let fieldClasses = 'field fluid'
    let inputClasses = 'ui fluid input '
    let messageType = ''
    let messageContent = ''
    let iconType = ''
    if (form.meta.error && form.meta.touched) {
        messageType = 'error'
        messageContent = form.meta.error
        fieldClasses += ' error'
        inputClasses += ' icon'
        iconType = 'error'
        if (form.meta.error === 'password_error') {
            messageContent = (
                <em>
                    <b>1 Uppercase</b> letter <br />
                    <b>1 Lowercase</b> letter <br />
                    <b>1 Number</b> <br />
                    At least <b>8 characters</b> long <br />
                </em>
            )
        }
    } else if (form.meta.touched) {
        inputClasses += ' icon'
        iconType = 'success'
    }
    return (
        <div className={fieldClasses}>
            <label>{form.label}</label>
            <div className={inputClasses}>
                <input {...form.input} autoComplete="off" type={form.type} placeholder={form.placeholder} />
                {this.renderIcon(iconType)}
            </div>
            {this.renderMessage(messageType, messageContent)}
        </div>
    )
}

When i throw a new error, the other one disappears. here are some images.

img1

img2

img3

5
  • First link of your image gives me access denied. Can you please send the accessible link so I can edit your post again? Commented Feb 14, 2019 at 4:12
  • drive.google.com/open?id=11K_QHG_c_YhweyrNiZ-cFdvyUP35Kefj Commented Feb 14, 2019 at 4:15
  • Thats a folder with the three images Commented Feb 14, 2019 at 4:15
  • Please show your component code where you have set your error hide/show code Commented Feb 14, 2019 at 5:36
  • Yeah sure, wait a moment. Commented Feb 14, 2019 at 17:37

2 Answers 2

2

I have found a not too dirty way to do it - in case anyone needs to use redux-form:

const asyncValidate = ( values, dispatch, props, field ) => {
    let validationPromises = [
        {
            field: "field1",
            request: axios.get(`validationUrl1/${values.field1}`),
            error: "Error message 1"
        },
        {
            field: "field2",
            request: axios.get(`validationUrl2${values.field2}`),
            error: "Error message 2"
        }
    ]

    let promisesResolved = validationPromises.map(promise => promise.request.catch(error => ({ error })));

    return axios.all(promisesResolved).then(
        (args) => {
            let errors = {};
            args.forEach( (r, idx ) => {
                if(!r.error){
                    errors[validationPromises[idx].field] = validationPromises[idx].error
                }
            })
            if(Object.keys(obj).length > 0){
               throw errors
            }
        }
    )
  }
  

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

Comments

0

This is gonna be brief.

Is a mess to do that with Redux-Form, so i better use Formik, is lighter, and just handle the necessary things, unless you need some features of redux-form, this is the way to go.

https://jaredpalmer.com/formik/docs/overview

1 Comment

You can pass a function to every field so you can validate it (it accepts promises).

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.