Dynamic checkbox validation with jquery-validator

3

I'm trying to validate a checkbox using the link plugin. The problem occurs that the field I'm validating does not exist until another predetermined field is selected. Therefore, validation passes even though this field does not exist. I did not get a way to make validation happen even though the field does not exist.

I tried this way:

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

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

    
asked by anonymous 24.02.2014 / 21:25

2 answers

2

The only solution that occurs to me is to check 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 submission of the form if the dynamic element exists, if not, we do something like presenting an error message in the appropriate place.

    
25.02.2014 / 01:06
0

Could you make the field always exist, leaving it invisible?

display: none;

If you can, then that would solve your problem.

    
24.02.2014 / 21:59