Add validators to fields
Adding validation rules is simple, and it's based around element ids. In order to add validation on a field, it needs to have an id.
// [field id].is([validator name]);
"name".is("required");
As mentioned earlier, this will have no effect unless you actually attach it to a form:
validate("name".is("required"));
Aliases
In order to make statements like these have a natural language like flow, the
is method is aliased to several others: isA,
isAn, has, hasA and hasAn.
Using these, we can make sure most statements make sense:
validate(
"name".is("required"),
"office_mail".isAn("email"),
"address".has("min-length", 4)
);
Adding more than one validator
To create robust forms we often need to validate individual fields with more
than one validator. Doing this is quite easy through and,
andIs, andIsA, andIsAn,
andHas, andHasA and andHasAn. These can
be chained at will:
validate(
"name".is("required").andIsA("word").andHas("min-length", 2),
"office_mail".isAn("email").andIs("required"),
"address".has("min-length", 4).and("max-length", 50)
);
Next: Add validators with parameters | Back to DSL features index