javascript – jQuery validate – skip_or_fill_minimum rule does not fire if used more than once

Question:

I have a form that is made up of four inputs and they are grouped in pairs.

The rule for this form is very simple, if I fill in one of the inputs in the pair, I have to fill in the other one too, or not fill any at all. To achieve this behavior I used the skip_or_fill_minimun method.

The HTML:

<div id="msgErros"></div>
<form>
    <label for="dataInicial">Data Inicial</label>
    <input type="text" name="filtro.dataInicial" id="dataInicial" class="datas" />
    <label for="dataFinal">Data Final</label>
    <input type="text" name="filtro.dataFinal" id="dataFinal" class="datas" />
    <br />
    <label for="tempoInicial">Tempo Inicial</label>
    <input type="text" name="filtro.tempoInicial" id="tempoInicial" class="tempos" />
    <label for="tempoFinal">Tempo Final</label>
    <input type="text" name="filtro.tempoFinal" id="tempoFinal" class="tempos" />
    <br />
    <button type="submit">Enviar</button>
</form>

Validation rules, messages and groups:

$("form").validate({
    rules : {
        "filtro.dataInicial": {
            skip_or_fill_minimum: [2, ".datas"]
        },
        "filtro.dataFinal": {
            skip_or_fill_minimum: [2, ".datas"]
        },
        "filtro.tempoInicial": {
            skip_or_fill_minimum: [2, ".tempos"]
        },
        "filtro.tempoFinal": {
            skip_or_fill_minimum: [2, ".tempos"]
        }
    },
    messages: {
        "filtro.dataInicial": {
            skip_or_fill_minimum: "Por favor, preencha ambos os campos de data ou nenhum deles."
        },
        "filtro.dataFinal": {
            skip_or_fill_minimum: "Por favor, preencha ambos os campos de data ou nenhum deles."
        },
        "filtro.tempoInicial": {
            skip_or_fill_minimum: "Por favor, preencha ambos os campos de tempo ou nenhum deles."
        },
        "filtro.tempoFinal": {
            skip_or_fill_minimum: "Por favor, preencha ambos os campos de tempo ou nenhum deles."
        }
    },
    groups : {
        grupoDatasAtendimentoSintetico : "filtro.dataInicial filtro.dataFinal",
        grupoTemposAtendimentoSintetico : "filtro.tempoInicial filtro.tempoFinal"
    },
    errorContainer : "#msgErros ul",
    errorLabelContainer : "#msgErros",
    wrapper : "li"
});

The problem happening is that if I fill one of the first two inputs the rule doesn't fire, the problem doesn't occur if I do it with the second pair. If I delete the second pair the rule runs perfectly so I think it's a bug. Here is a fiddle .

I've read about this method and require_from_group causing problems that simply prevent other methods from running, but this bug was supposedly fixed in version 1.11.1 , which is what I'm using in my project and in fiddle for both the plugin itself and his additional methods.

The problem only happens when the user fills in one of the fields in the first pair. Does anyone know if this is another bug? I have not found anything related to this on the GitHub issue tracker for this plugin.

UPDATE:

Make an entry in the plugin's issue tracker at: https://github.com/jzaefferer/jquery-validation/issues/1008 .

Answer:

The problem you present has been resolved in a recent commit .

See your Fiddle updated . I removed the external dependencies, copied and pasted the code from http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js into the beginning of JavaScript, then pasted the code from http: //ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js – and finally I left your JavaScript unchanged.

Then I got the latest "skip_or_fill_minimum" code here: https://github.com/jzaefferer/jquery-validation/blob/master/src/additional/skip_or_fill_minimum.js

And finally, I pasted this code overwriting the previous version that was in Fiddle.

With that, the error is gone! From which it is concluded that the bug actually existed but has already been fixed.

Scroll to Top