Search code examples
validationtypescriptaurelia

Conditional Aurelia Validation on Visibility of Element


I'm trying to work out some license subscription form with aurelia and aurelia validation plugin. I have a fieldset for personal information where most of them are required and get validated by aurelia validation.

Now I also have a fieldset for credit card information and for billing address with also fields that are required and validated. The thing is, they only get displayed when the user picks the subscription radio button. I have all the required fields in the ValidationRules, how can I tell aurelia that it should only validate the ones that are currently visible?


Solution

  • You can use conditional validation with the when fluent method. Here is an example from the Aurelia Docs.

    ValidationRules .ensure('email') .email() .required() .when(order => order.shipmentNotifications) .withMessage('Email is required when shipment notifications have been requested.');