Closed Benjen closed 8 years ago
Simplest way would be to make a custom validator. See docs and included validators for direction. If you used it alot you could parameterise it and implement something like: requiredIfShown(parentSelector)
(example use of validator arguments here).
I had a quick play with addRules() method. It seems that my custom rule is skipped if I don't use required rule on the field. THe following is my custom rule.
jQuery.verify.addRules({
beng: function(r) {
// Add code here.
return true;
}
});
If I apply it to a field <input id="name" name="name" type="text" data-validate="beng" />
I get the following message in console verify.js: FieldExecution: #2: [name] skip (not required)
.
The only way I can get the custom rule to work is if I also add the required
validator (e.g. <input id="name" name="name" type="text" data-validate="required,beng" />
). Problem is I don't want to use required
, as my custom rule is meant to be used as a "conditional required" validator (e.g. the field is only required if some condition exists.)
Any advice on how to get around this issue?
Ah I think that error appears if the field is empty. Though you want to implement your own required validator so thats what you want to test.
Okay just had a look through the source (its been a long time) and found:
//special required case
if(/required/.test(result.name))
required = true;
So, if you call your rule requiredBeng
it should work.
That did the trick. Thank you.
I am trying to work out if there is an easy method of adding conditional verification. For example, I have certain form fields which are only required if certain other fields are visible. Is there some way of conditionally setting a field as required when some other condition is met (e.g. the parent form fieldset is visible, etc.)?