8
I have a form and this has three pairs of start/end dates. For each pair I validate if both dates are filled in or none of them is filled in, because it is not allowed to fill in the start date and not the end date and vice versa, moreover, I can not send the form without at least one of the completed pairs.
Imagine the user entering dates only at start dates , but none at end dates, I have to show you three error messages, one for each pair. But if the user enters no data, I should send only an error message.
The problem is that there are four groups, one for each pair and one for the whole form, in other words, the first three pairs are subgroups of the largest.
If I put the larger group in my code last, I only get an error message if the user sends a blank form but only get an error message if the user enters only the start or end dates as well. If I put the larger group first, I get three error messages when the user enters only the start dates, which is right, but I also get three error messages equal if the user sends an empty form.
HTML:
<div id="msgErros"></div>
<form>
<label for="dataInicialSolicitacao">Solicitação Inicial</label>
<input type="text" class="data skipOrFillDatasSolicitacao atLeastOneRequired" id="dataInicialSolicitacao" value="" size="12" name="filtro.dataInicialSolicitacao" maxlength="10" />
<label for="dataFinalSolicitacao">Solicitação Final</label>
<input type="text" class="data skipOrFillDatasSolicitacao atLeastOneRequired" id="dataFinalSolicitacao" value="" size="12" name="filtro.dataFinalSolicitacao" maxlength="10" />
<br />
<label for="dataInicialLiberacao">Liberação Inicial</label>
<input type="text" class="data skipOrFillDatasLiberacao atLeastOneRequired" id="dataInicialLiberacao" value="" size="12" name="filtro.dataInicialLiberacao" maxlength="10" />
<label for="dataFinalLiberacao">Liberação Final</label>
<input type="text" class="data skipOrFillDatasLiberacao atLeastOneRequired" id="dataFinalLiberacao" value="" size="12" name="filtro.dataFinalLiberacao" maxlength="10" />
<br />
<label for="dataInicialInternacao">Internação Inicial</label>
<input type="text" class="data skipOrFillDatasInternacao atLeastOneRequired" id="dataInicialInternacao" value="" size="12" name="filtro.dataInicialInternacao" maxlength="10" />
<label for="dataFinalInternacao">Internação Final</label>
<input type="text" class="data skipOrFillDatasInternacao atLeastOneRequired" id="dataFinalInternacao" value="" size="12" name="filtro.dataFinalInternacao" maxlength="10" />
<br />
<button>Consultar</button>
<button type="reset">Limpar</button>
</form>
Javascript:
$(".data").mask("99/99/9999").datepicker();
$("form").validate({
rules: {
"filtro.dataInicialSolicitacao": {
skip_or_fill_minimum: [2, ".skipOrFillDatasSolicitacao"],
require_from_group: [2, ".atLeastOneRequired"]
},
"filtro.dataFinalSolicitacao": {
skip_or_fill_minimum: [2, ".skipOrFillDatasSolicitacao"],
require_from_group: [2, ".atLeastOneRequired"]
},
"filtro.dataInicialInternacao": {
skip_or_fill_minimum: [2, ".skipOrFillDatasInternacao"],
require_from_group: [2, ".atLeastOneRequired"]
},
"filtro.dataFinalInternacao": {
skip_or_fill_minimum: [2, ".skipOrFillDatasInternacao"],
require_from_group: [2, ".atLeastOneRequired"]
},
"filtro.dataInicialLiberacao": {
skip_or_fill_minimum: [2, ".skipOrFillDatasLiberacao"],
require_from_group: [2, ".atLeastOneRequired"]
},
"filtro.dataFinalLiberacao": {
skip_or_fill_minimum: [2, ".skipOrFillDatasLiberacao"],
require_from_group: [2, ".atLeastOneRequired"]
}
},
groups: {
datasSolicitacao: "filtro.dataInicialSolicitacao filtro.dataFinalSolicitacao",
datasLiberacao: "filtro.dataInicialLiberacao filtro.dataFinalLiberacao",
datasInternacao: "filtro.dataInicialInternacao filtro.dataFinalInternacao",
todos: "filtro.dataInicialSolicitacao filtro.dataFinalSolicitacao filtro.dataInicialLiberacao filtro.dataFinalLiberacao filtro.dataInicialInternacao filtro.dataFinalInternacao"
},
errorContainer: "#msgErros ul",
errorLabelContainer: "#msgErros",
wrapper: "li"
});
I created a fiddle with the larger group at the end. To test it just enter start dates and press "Query", you will only get one message when I need three. Then press "Clear" and then "Query" and you will get a message just what is correct.
And here’s a variation of the first fiddle with the larger group at the beginning. To test it just enter start dates and press "Query", you will get three messages, one for each line and this is correct. Then press "Clear" and then "Query" and you will receive three messages and this is not correct.
Any idea to make the behavior correct occurs for both cases?
I use jQuery Validate 1.11.1 with jQuery 1.11.0.
I saw your question in the meta and your post in English in the OS. Hint: try to simplify the code more before posting here. If you can create a minimal example to explain the problem, it’s easier for people to help because they don’t need to read and understand larger code. Bonus: in the process of trying to create this example, we often discover the solution on our own.
– bfavaretto
I get it, I’m going to take out a lot of stuff that has absolutely nothing to do with my problem like the reset of the form, the ignored rules, taking out the datepicker that does not influence the problem and then the onfocusout and so on. Thank you for the touch. In time, did you manage to look at the fiddles? Do you have any idea what the problem might be? And congratulations on correlating these two questions with the one I did on meta, quick and logical reasoning.
– Philippe Gioseffi
I received a Warning about it, but did not know how to fix it. Thanks for the guidance! In time, why should we do it this way?
– Philippe Gioseffi
I couldn’t get a good look at the code because it was too big :)
– bfavaretto
Don’t take the datepicker off, it might come in handy... And by the way, to use github links, take the first
.
(point). So use:https://rawgithub.com/digitalBush/
instead ofraw.github
http://jsfiddle.net/LevK6/2/– Sergio
About the
git
, take a look here: https://rawgithub.com/– Sergio
I understand about git and I’m already editing my code here and in the two fiddles.
– Philippe Gioseffi
@bfavaretto code in question and fiddle modified according to your instructions. You are better now?
– Philippe Gioseffi
It got better yes! I hope an answer appears, I unfortunately do not know the jq.validate well enough to help,
– bfavaretto
Anyway helped a lot in other aspects, @bfavaretto.
– Philippe Gioseffi