I try to add validation dynamically but validate method returns only the first error.
My goal is to parse a json with all validation rules from the server and add validation dynamically.
my html:
<input id="myInput1" value.bind="myInput1 & validateOnChangeOrBlur" errors.bind="myInput1Errors"/>
<input id="myInput2" value.bind="myInput2 & validateOnChangeOrBlur" errors.bind="myInput2Errors"/>
<button click.delegate='validate()'>validate</button>
<ul if.bind="controller.errors">
<li repeat.for="error of controller.errors">
${error.message}
</li>
</ul>
My .ts:
attached() {
let rules = [];
//my real code is a for loop for adding dynamically
rules.push(ValidationRules
.ensure('myInput1')
.required()
.rules[0]);
rules.push(ValidationRules
.ensure('myInput2')
.required()
.rules[0]);
this.controller.addObject(this, rules);
}
validate() {
this.controller.validate()
.then(result => {
if (result.valid) {
//
} else {
//
}
});
}
When I blur (tab navigation), validate method shows errors correctly, stacking them one after another.
When I call validate, it shows only the first one.
However, if I do this statically, it works (both blur and validate):
ValidationRules
.ensure('myInput1')
.required()
.ensure('myInput2')
.required()
.on(this);
So what’s wrong with adding it dynamically?
Thanks.