Reputation: 11
With aurelia validation, is there a way to require one OR another form field? (see commented code)?..or guidance on assigning rules to variables as the documentation wasn't very in depth (Junior Dev Here). I was originally trying to create a custom rule to handle it but was having trouble getting it to work properly. The custom rule could then change the rule assigned to the var depending on field input.
const initialRules = ValidationRules
.ensure('newContactFirstName').required()
.ensure('newContactLastName').required()
// if phone number not filled out require email
.ensure('newContactEmail').required().then().email()
//if email not filled out rquire phone number
.ensure('newContactAreaCode').required().then().satisfiesRule('isNumber').then().minLength(3).then().maxLength(3)
.ensure('newContactPrefix').required().then().satisfiesRule('isNumber').then().minLength(3).then().maxLength(3)
.ensure('newContactSuffix').required().then().satisfiesRule('isNumber').then().minLength(4).then().maxLength(4)
Upvotes: 0
Views: 264
Reputation: 5688
You'll want to use custom rules. Try something like this:
ValidationRules.ensure("newContactEmail").satisfies((email, myObject) => {
if (email)
{
return true;
}
else
{
return myObject.newContactAreaCode != ""
&& myObject.newContactPrefix != ""
&& myObject.newContactSuffix != ""
}
})
.on(myObject)
Basically, If the email has a value, the validation passes, else check to make sure the phone number fields have values.
I'd also advise you to take a look here.
Upvotes: 1