testfields.v7.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // ---------------------------------------------------------------------------------------------------------------------
  2. // TESTS: Async Validator
  3. // ---------------------------------------------------------------------------------------------------------------------
  4. import { FormControl, Validators, ValidationErrors } from '@angular/forms';
  5. // For the simple async validator
  6. import { Observable, of } from 'rxjs';
  7. import { delay, map } from 'rxjs/internal/operators';
  8. const testAsyncValidator = (fc: FormControl): Observable<ValidationErrors> => {
  9. return of(fc).pipe(delay(2000)).pipe(map(_fc => {
  10. console.log('Async validator got', _fc.value);
  11. return _fc.value.trim() !== '42' ? { isNot42: true } : null;
  12. }));
  13. };
  14. const model = {
  15. nonValidatedField: '',
  16. dynaformtest: {
  17. validatedField: ''
  18. }
  19. };
  20. const meta = {
  21. dynaformtest: {
  22. meta: {
  23. validatedField: {
  24. label: 'What\'s 30 + 12 ?',
  25. validators: Validators.required,
  26. asyncValidators: testAsyncValidator,
  27. valFailureMessages: {
  28. 'required': 'This field is required',
  29. 'is42': 'The only correct answer is 42'
  30. }
  31. }
  32. }
  33. }
  34. };
  35. export { model, meta };