Dynamic checkbox validation with jquery-Validator

Asked

Viewed 1,081 times

3

I’m trying to validate a checkbox using the plugin http://jqueryvalidation.org/. The problem is that the field I am validating it does not exist until another preset field is selected. Therefore, the validation passes even without this field. I couldn’t find a way to make the validation happen even if the field doesn’t exist.

I tried so:

$('#form_novo').validate({
 rules: {
    'bnd[]': {
        required: true
    },
    'responsavel[]': {
        required: true
    },
    'etapa[]': {
        required: true
    },
}

This responsible field exists only after the field has been selected bnd. Does anyone know how to solve?

2 answers

2


The only solution I can think of is for you to control the existence of this dynamic field in submitHandler and act accordingly:

Example in Jsfiddle

$('#form_novo').validate({
    rules: {
        'bnd[]': {
            required: true
        },
        'responsavel[]': {
            required: true
        },
        'etapa[]': {
            required: true
        },
    },
    submitHandler: function(form) {
        // estou a assumir que tem a classe "responsavel"
        if ($(form).find('.responsavel').size()>=1) {
            form.submit();
        } else {
            alert("crap");
        }
    }
});

What is being done is to allow the form submission if the dynamic element exists, if not, we do something like present an error message in the appropriate location.

0

You could make the field always exist, leaving the same invisible?

display: none;

If you can, then that would solve your problem.

  • I can’t, it’s a dynamic field that depends on the choice of bnd

Browser other questions tagged

You are not signed in. Login or sign up in order to post.