|
@@ -0,0 +1,41 @@
|
|
|
+// TESTS: Async Validator
|
|
|
+
|
|
|
+import { FormControl, Validators, ValidationErrors } from '@angular/forms';
|
|
|
+
|
|
|
+// For the simple async validator
|
|
|
+import { Observable } from 'rxjs/Observable';
|
|
|
+import 'rxjs/add/observable/of';
|
|
|
+import 'rxjs/add/operator/delay';
|
|
|
+import 'rxjs/add/operator/map';
|
|
|
+
|
|
|
+const testAsyncValidator = (fc: FormControl): Observable<ValidationErrors> => {
|
|
|
+ return Observable.of(fc).delay(2000).map(fc => {
|
|
|
+ console.log('Async validator got', fc.value);
|
|
|
+ return { is42: fc.value === '42' };
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+const model = {
|
|
|
+ nonValidatedField: '',
|
|
|
+ dynaformtest: {
|
|
|
+ validatedField: ''
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const meta = {
|
|
|
+ dynaformtest: {
|
|
|
+ meta: {
|
|
|
+ validatedField: {
|
|
|
+ label: 'What\'s 30 + 12 ?',
|
|
|
+ validators: Validators.required,
|
|
|
+ asyncValidators: testAsyncValidator,
|
|
|
+ valFailureMessages: {
|
|
|
+ 'required': 'This field is required',
|
|
|
+ 'is42': 'The only correct answer is 42'
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+export { model, meta };
|