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

Asked

Viewed 208 times

6

I have a form that consists of four inputs and they are grouped in pairs.

The rule for this form is very simple, if I fill in one of the pair’s inputs, I have to fill in the other as well or fill out none. To achieve this behavior I used the method skip_or_fill_minimun.

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 is that if I fill one of the first two inputs the rule is not triggered, the problem does not occur if I do this with the second pair. If I delete the second pair the rule runs perfectly, then I think it’s a bug. Here’s a fiddle.

I 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 on fiddle both for the plugin itself and its 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 found nothing related to this in Github Issue tracker of this plugin.

UPDATING:

Create a plugin Issue tracker entry in the address: https://github.com/jzaefferer/jquery-validation/issues/1008.

  • Are you having this problem in a specific browser? I tried your fiddle on Chrome and Firefox and could not play the problem.

  • I use the latest Chrome, FF and IE. And how did you test? Fill in the first or second field only and submit the form.

  • Okay, I get it. The message warning the problem appears, but still he lets you submit the form.

  • Exactly that, he warns, but submits and should not submit.

1 answer

3


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

Behold your updated Fiddle. I removed the external dependencies, copied and pasted the code from http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js at the beginning of Javascript, then pasted the code of http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.js - and finally kept your Javascript unchanged.

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

And finally, I pasted this code replacing the previous version that was on Fiddle.

With that, the error disappeared! Where it is concluded that the bug actually existed but has already been fixed.

  • What was wrong with my code? What do I have to do to get the form validated the way I need it?

  • @Jbruni so I understood the correction of the method you placed in my question in the OS in English is not the one in version 1.11.1, correct?

  • Version 1.11.1 is 22 March 2013 (https://github.com/jzaefferer/jquery-validation/tree/1.11.1). The commit correcting the error was dated 27 August 2013 (https://github.com/jzaefferer/jquery-validation/commit/e5207d63a831fdfa30ea6927906288ae60336c76)

  • There’s nothing wrong with your code. It’s a bug. The bug is present in version 1.11.1 - either you expect a new version, or you apply the fix on your own.

  • To apply the correction on your own, you have to replace the snippet indicated in the answer (the most recent "skip_or_fill_minimum" code). Use the un-minimized version of "Additional-methods.js" (and then minimize yourself with "uglify.js" if you want to).

  • 1

    @Jbruni, thank you for helping.

  • In time, I must copy the require_from_group as well?

  • I think it’s a good idea. (In this case, it’s not necessary, because you are only using "skip_or_fill_minimum", so it makes no difference. However, we know that the "require_from_group" has also been corrected, so it’s a good idea.)

  • I use in another form yes, and as the problem is the same for both methods I will update the two.

Show 4 more comments

Browser other questions tagged

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