validation
validation copied to clipboard
Validation support for subproperties
I'm submitting a feature request
Current behavior:
Given the follwing template code: <input type="hidden" value.bind="newEntry.address.street1 &validate" /> <input type="hidden" value.bind="newEntry.address.street2 &validate" /> <input type="hidden" value.bind="newEntry.address.city &validate" />
And the current validation code (using aurelia-validatejs)
this.validationRules = new ValidationRules() .ensure("address.street1").required().length({minimum:5, maximum:50}). .ensure("address.street2").length({minimum:5, maximum:50}). .ensure("address.city").length({minimum:5, maximum:50}). ... on(this.newEntry);
The validation controller passes to the validator for (street1, street2 and city) the propertyName : street1,street2 or city (depending on the field being validated) and the following object: { street1:"", street2:"". city:"", }
Expected/desired behavior:
Expected behaviour would to pass the entire object being validated not just the object containing the sub-property, e.g { address: { street1:"", street2:"". city:"", } } Also, the full "path" to the property from the object "root" e.g. address.street1 instead of "street1"
What is the motivation / use case for changing the behaviour?
The motivation for this change is due to how aurelia-validationjs stores the rules as metadata in the containing object (newEntry in this case).
It is unable to find any rules when the address object is passed to it.
Also we will need the full path of the property (e.g address.street1) as the rules for sub-properties are defined as, for example, "address.street1" not "street1". It needs the path to be able to find the correct rule.
that'll be useful to have
todo: consider https://github.com/aurelia/validation/issues/332 when implementing this.
Wouldn't it be more appropriate to create separate validation rules for Address and then allow a .valid on those rules? This is how NHibernate Validator works. I wouldn't want to define rules for a reusable class everywhere it is associated with another class.
I really need this in my application.
NHibernate Validator examples
//to validate a one-to-one association
Define(x => x.PropertyReferencingClass)
.IsValid();
//to validate a one-to-many association
Define(x => x.SomePropertyCollection)
.HasValidElements();
Curious why this is on-hold? It seems like if binding supports binding paths then validation should as well!
Is there an update on the status of this, is this still not supported? Thanks.
Also related: #442