Is it possible to create a validator which can use multiple values to decide if my field is valid?
e.g. if the customer's preferred contact method is by email then the email field should be required.
Thanks.
Updated with example code...
import {Component, View} from 'angular2/angular2';
import {FormBuilder, Validators, formDirectives, ControlGroup} from 'angular2/forms';
@Component({
selector: 'customer-basic',
viewInjector: [FormBuilder]
})
@View({
templateUrl: 'app/components/customerBasic/customerBasic.html',
directives: [formDirectives]
})
export class CustomerBasic {
customerForm: ControlGroup;
constructor(builder: FormBuilder) {
this.customerForm = builder.group({
firstname: [''],
lastname: [''],
validateZip: ['yes'],
zipcode: ['', this.zipCodeValidator]
// I only want to validate using the function below if the validateZip control is set to 'yes'
});
}
zipCodeValidator(control) {
if (!control.value.match(/\d\d\d\d\d(-\d\d\d\d)?/)) {
return { invalidZipCode: true };
}
}
}