The Topincs form can be customized to validate the user input. This is achieved with a Topincs specific constraint type, the compound constraint. The validation is performed on the client side, so the code is written in JavaScript.
A compound constraint uses JavaScript to define elaborate validation rules which may span more than one statement. It is composed of:
Santa Claus does not want children to get hurt by using their new gifts. One problem in the past was that bikes were delivered to toddlers which were too young. He wants to prevent this from happening again. We realize this by creating a compound constraint.
// 1274 is the system id of bike.
function (age, gift) {
return age < 4 && gift == 1274;
}
// Alternatively throw a ValidationError.
function (age, gift) {
if (age < 4 && gift == 1274) {
throw new ValidationError();
}
}
By default the validation message will be displayed in the first statement of the paragraph of the last constraint of the argument order. Since you control how they are passed in this should suffice. In rare cases it is necessary to blame another statement for the error. You can achieve this by passing the information to the ValidationError
constructor.
function (age, gift) {
if (age < 4 && gift == 1274) {
throw new ValidationError(0);
}
}
function (count, items) {
if (count < items.length) {
throw new ValidationError([1, items.length]);
}
}
A compound constraint can only blame exactly one field. If input occurred, that should display several additional fields, one has to use setState
and removeState
. This may be combined with throwing a ValidationError
.
// A boolean field created by a topic occurrence constraint is passed in.
function (flag) {
if (flag === true) {
this.setState("flag");
} else {
this.removeState("flag");
}
}
// Intially hides some fields.
.t555, .t522-523 {
display: none;
}
// And display them only if in 'flag' state.
.flag {
.t555, .t522-523 {
display: block !important;
}
}
This page cannot be displayed in your browser. Use Firefox, Opera, Safari, or Chrome instead.
Saving …