是否可以創(chuàng)建一個(gè)可以使用多個(gè)值來確定我的字段是否有效的驗(yàn)證器?例如,如果客戶首選的聯(lián)系方式是通過電子郵件,則必須填寫電子郵件字段。謝謝。更新了示例代碼... 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 }; } } }
- 3 回答
- 0 關(guān)注
- 775 瀏覽
添加回答
舉報(bào)
0/150
提交
取消