I have an issue with nested validation where the nested object needs access to the parent object for the .when
statement.
ValidationRules
.ensure('type').required().when(thing => thing.parent.category !== 'note').on(this.thing)
.ensure('category').required();
In this code thing.parent
clearly doesn’t exist but I’d like the when clause to have access to the value of category. I have a workaround in place but it seems like it does not follow the spirit of how Validation is supposed to work (I’m using a closure).
A better real world example would be a user that may or may not want to receive postal mail. I would only validate the address if the form had the box checked for “receiveMail.”
Can this be done with the current state of the validation plugin or do I need to file a bug?
var user = {
first: 'Dave',
last: 'Jensen',
receiveMail: true,
address: {
street: "123 Main St.",
city: "Somewhere",
region: "California",
country: "USA",
postalCode: "99999"
}
@MaximBalaganskiy gave an answer on the gitter chat
you can setup up separate rules for user and address
for address ones do not consider receiveMail
for the user have a rule:
.when(x=>x.receiveMail).satisfies(x=>this.validationController.validate(addressRules,x.address))
not correct code, but should give you an idea
1 Like