6
I got three fields, two inputs text
and between them a select
. If one of the first two is filled in the other becomes mandatory. The problem is that when a single value, in the latter case, of the select is filled the three become mandatory.
It is possible to have dynamic rules with this plugin, ie when checking the value of select make the third field mandatory?
HTML
<div id="msgErros"></div>
<form>
<label for="tempoInicial">Tempo Inicial</label>
<input type="text" name="filtro.tempoInicial" size="8" value="" id="tempoInicial" class="tempo" />
<br />
<label for="operadorTempo">Operador de Tempo</label>
<select name="filtro.valorOperadorTempo" id="operadorTempo" class="tempo">
<option value="">-- Selecione --</option>
<option value="1">> - Maior</option>
<option value="2">< - Menor</option>
<option value="3">Entre</option>
</select>
<br />
<label for="tempoFinal">Tempo Final</label>
<input type="text" name="filtro.tempoFinal" size="8" value="" id="tempoFinal" />
<br />
<button>Enviar</button>
</form>
Javascript
$.validator.addMethod("skip_or_fill_minimum", function (value, element, options) {
var $fields = $(options[1], element.form),
$fieldsFirst = $fields.eq(0),
validator = $fieldsFirst.data("valid_skip") ? $fieldsFirst.data("valid_skip") : $.extend({}, this),
numberFilled = $fields.filter(function () {
return validator.elementValue(this);
}).length,
isValid = numberFilled === 0 || numberFilled >= options[0];
// Store the cloned validator for future validation
$fieldsFirst.data("valid_skip", validator);
// If element isn't being validated, run each skip_or_fill_minimum field's validation rules
if (!$(element).data("being_validated")) {
$fields.data("being_validated", true);
$fields.each(function () {
validator.element(this);
});
$fields.data("being_validated", false);
}
return isValid;
}, jQuery.validator.format("Please either skip these fields or fill at least {0} of them."));
$("form").validate({
errorContainer: "#msgErros ul",
errorLabelContainer: "#msgErros",
wrapper: "li",
rules: {
"filtro.tempoInicial": {
skip_or_fill_minimum: [2, ".tempo"]
},
"filtro.valorOperadorTempo": {
skip_or_fill_minimum: [2, ".tempo"]
}
},
groups: {
tempos: "filtro.tempoInicial filtro.valorOperadorTempo"
},
onfocusout: function (element, event) {
var classeCSSElemento = $(element).attr("class");
var classeCSSElementoIndefinida = typeof classeCSSElemento === "undefined";
if (classeCSSElementoIndefinida || (!classeCSSElementoIndefinida && !classeCSSElemento.contains("tempo"))) {
$.validator.defaults.onfocusout.call(this, element, event);
}
}
});
Exactly what I needed, thank you!
– Philippe Gioseffi
One last question, do you know how to put this input to a group and dynamically pull it out?
– Philippe Gioseffi
@Philippegioseffi: from what I could see in the documentation and in the SOEN there is no way to dynamically change the group. I’m limited access, (no google, and some other sites), so it gets kind of complicated. =\
– Miguel Angelo
Someone screwed up in the Infra sector here! hehe
– Miguel Angelo
Good, then I will leave the group fixed with the three fields, I will leave now only the dynamic messages, thanks again for the help.
– Philippe Gioseffi
Arrange... I’ll keep trying later when the problem here is solved.
– Miguel Angelo