Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validateBeforeSubmitting doesn't allow triggering async validator #109

Open
bepenelle opened this issue Feb 13, 2023 · 8 comments
Open

validateBeforeSubmitting doesn't allow triggering async validator #109

bepenelle opened this issue Feb 13, 2023 · 8 comments

Comments

@bepenelle
Copy link

I set validateBeforeSubmitting to true. Thanks to that settings, the synchronous validators do trigger when the field value changes. However, my async validator is not triggered when the field value changed.

Here is my code :

$(function(){
    const validation = new JustValidate('#signupForm', {
        validateBeforeSubmitting: true,
    });

    validation
        .addField('#pseudo', [
            {
                rule: 'required',
                errorMessage: 'Field is required'
            },
            {
                rule: 'minLength',
                value : 3,
                errorMessage: 'Minimum 3 characters'
            },
            {
                rule: 'maxLength',
                value : 16,
                errorMessage: 'Maximum 16 characters'
            },
            {
                validator: function(value) {
                    return function() {
                        return $.getJSON("member/pseudo_available_service/" + value);
                    }
                },
                errorMessage: 'Name already exists',
            },
        ], { successMessage : 'Looks good !'})
        ...

How can I solve this ?

@horprogs
Copy link
Owner

Async validators not triggered after inputs changing (this applies to validateBeforeSubmitting as well). The reason is you usually don't want to call API endpoints for every change, it creates unnecessary load for the backend.

Currently async validators triggered only when you submit the form.

@horprogs
Copy link
Owner

I can suggest a workaround to use onValidate callback (it triggered every time when input changes), call your endpoint there and use showErrors method

@bepenelle
Copy link
Author

Is there a workaround? This is some kind of feature request in my case. It should be nice to have a flag in the settings for that and let the developer decide if the extra load is acceptable or not.

@horprogs
Copy link
Owner

yeah makes sense to add this flag. I described a workaround above

@bepenelle
Copy link
Author

thanks, I will try the workaround

@bepenelle
Copy link
Author

looking forward for the flag in the settings ;-)

@bepenelle
Copy link
Author

The workaround you proposed works great! Thx

.onValidate(async function(event) {
    const res = await $.getJSON("member/pseudo_available_service/" + $("#pseudo").val());
    if (!res)
        this.showErrors({ '#pseudo': 'The pseudo is already used' })
})

However, if you manage to add a flag in the settings to handle that use case in a more "standard" way, I'm most certainly interested.

@galabl
Copy link

galabl commented Apr 18, 2023

Any update, eta on this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants