aurelia / validatejs

Enables expressive validation using decorators and/or a fluent API.
MIT License
22 stars 23 forks source link

conditional validation #108

Closed adarshpastakia closed 8 years ago

adarshpastakia commented 8 years ago

In my form i have two address entries one for home and one for business, the business address entry is controlled by a checkbox. So if a user enables the business address entry i need to validate the required fields, if not i need to skip the validation. Maybe something like an if decorator that returns true/false, or checks a related property.

<p>Address: <input value.bind="homeAddress & validate"/></p>
<p>City: <input value.bind="homeCity & validate"/></p>

<p><input type="checkbox" checked.bind="hasWorkAddress"/> Work Address</p>
<p>Address: <input disabled.bind="!hasWorkAddress" value.bind="workAddress & validate"/></p>
<p>City: <input disabled.bind="!hasWorkAddress" value.bind="workCity & validate"/></p>
@required homeAddress='';
@required homeCity='';

hasWorkAddress=false;

// Check property
@if('hasWorkAddress')
@required 
workAddress='';
// Or return true/false from a callback method
@if(()=>this.hasWorkAddress})
@required 
workCity='';
EisenbergEffect commented 8 years ago

I don't think that we are going to add conditional validation. Instead, I think this is better handled by a simple custom validation rule. @jdanyow Can you provide some advice here?

adarshpastakia commented 8 years ago

@EisenbergEffect This isn't exactly a custom validation situation, it more like having a set of properties whose validation rules need to be applied only when a certain condition is met. Something like an online purchase form where the 'Shipping address' properties are required, and if the user unchecked 'Billing address same as shipping' then i need to validate the billing address, or the payment type where the user can choose 'Cash on Delivery'/'Credit Card', incase the user chooses COD i don't really need to validate credit card for required and luhn algorithm validation, but if the user chooses CreditCard then the related properties need to be validated. As for having a custom validation to check against luhn algorithm is not an issue, i have created custom validation rules and decorators.

bhatnagar-ankur commented 8 years ago

+1

ajayvikas commented 8 years ago

+1

jdanyow commented 8 years ago

this was added in the last release