I am trying to use aurelia-validation
for client-side validation. Though the client-side validation works quite well, it forces reevaluation of the custom elements in the view. For example, a typical flow is as follows (the order in which the custom-elements are detached
/“unbind
ed” may differ from time to time):
validation starting
custom-element#1 detached
custom-element#2 detached
custom-element#3 detached
custom-element#2 unbind
custom-element#3 unbind
custom-element#1 unbind
validation ended
custom-element#1 bind
custom-element#2 bind
custom-element#1 attached
....
Naturally, re-activating the custom elements means also getting the data from the server which are needed in-order to render the elements in a meaningful way (for example, options in a dropdown, or items in a list, and so on).
Some more detail about the application: The validation rules are defined on server side (WebAPI), and an endpoint can deliver the rules for a “Entity” type. These rules are then translated to aurelia-validation
rules on client-side. And then it is simple matter of using those rules with my target entity object (which can be of any “Entity” type, depending on the scenario; naturally rules are fetched accordingly). The example code looks like below:
this.clientRules = this.ruleAdapter.toAureliaRules(validationRules);
this.validationController.addObject(this.targetEntity, this.clientRules);
// and finally invoke validate when required
this.validationController.validate()
Please note that targetEntity
is being used by the custom elements as well.
Thinking the targetEntity
is being modified during validate
, I also tried with following code:
const dummyEntity = JSON.parse(JSON.stringify(this.entity));
this.validationController.validate({ object: dummyEntity, rules: this.clientRules });
// naturally, while trying this code I removed the addObject call
However, this too didn’t work
I am not sure how to isolate the problem and look for the root cause. I have posted this question in StackOverflow as well. Any suggestion in either of these two places would be really great.