嘗試:
// import here your EqualsValidator
import { EqualsValidator} from '...';
...
...
...
this.mainForm = this.fb.group({
fromtotal: [0, [Validators.Required]],
toTotal: [0, [Validators.Required]],
option: [value, Validators.required]
}, {validator: EqualsValidator.equals('fromtotal', 'toTotal')});
equals.validator.ts:
import { FormGroup } from '@angular/forms';
export class EqualsValidator {
/**
* compares 2 values
* */
static equals(valueControlNameA: string, valueControlNameB: string) {
return (group: FormGroup): {[key: string]: any} => {
// get values
let valueA = group.get(valueControlNameA).value;
let valueB = group.get(valueControlNameB).value;
return valueA === valueB ? null : {equals: true};
}
}
}
我需要自定義驗證程序來做到這一點。 – user2401393