Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. this.myForm = new FormGroup({
  2. name: new FormControl('', Validators.minLength(3));
  3. city: new FormGroup({
  4. cityOne: new FormControl('', Validators.minLength(3)),
  5. cityTwo: new FormControl('', Validators.minLength(3))
  6. }, this.validateEqualCities)
  7. });
  8.  
  9. validateEqualCities(formGroup: FormGroup) {
  10. return (control: AbstractControl): { [key: string]: any } => {
  11. if (formGroup.controls['cityOne'].value && formGroup.controls['cityTwo'].value && formGroup.controls['cityOne'].value !== formGroup.controls['cityTwo'].value) {
  12.  
  13. formGroup.controls['cityOne'].setErrors({ 'equalCities': true }, { emitEvent: true });
  14. formGroup.controls['cityTwo'].setErrors({ 'equalCities': true }, { emitEvent: true });
  15.  
  16. return { 'equalCities': false };
  17.  
  18. } else {
  19. formGroup.controls['cityOne'].updateValueAndValidity({ onlySelf: true, emitEvent: false });
  20. formGroup.controls['cityOne'].updateValueAndValidity({ onlySelf: true, emitEvent: false });
  21. }
  22.  
  23. return null;
  24. };
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement