ronaldkwandy

Untitled

Jan 14th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public validate() {
  2.     try {
  3.       const ngControl: NgControl | NgModel = this.injector.get(NgControl);
  4.       let errors: ValidationErrors = {};
  5.       if (ngControl instanceof NgModel) {
  6.         if (this.input.valid) {
  7.           this.errorState = false;
  8.         } else {
  9.           this.errorState = true;
  10.         }
  11.  
  12.         errors = this.input.errors;
  13.       } else {
  14.         if (ngControl.invalid) {
  15.           this.errorState = true;
  16.         } else {
  17.           this.errorState = false;
  18.         }
  19.  
  20.         errors = ngControl.errors;
  21.       }
  22.  
  23.       for (const key in errors) {
  24.         if (key) {
  25.           const keyMessage = ObjectHelper.composeMessageKey(this.message, this.errorKey, key);
  26.           const params = ObjectHelper.composeMessageParams(this.message, key, errors);
  27.           if (params) {
  28.             this.translateService.get(keyMessage, params).subscribe(
  29.               (result) => {
  30.                 this.errorMessage = result;
  31.               }
  32.             );
  33.           } else {
  34.             this.translateService.get(keyMessage).subscribe(
  35.               (result) => {
  36.                 this.errorMessage = result;
  37.               }
  38.             );
  39.           }
  40.           break;
  41.         }
  42.       }
  43.     } catch (exception) {
  44.       console.warn('[TextInput] Forms / NgModel not defined!', exception);
  45.     }
  46.   }
Advertisement
Add Comment
Please, Sign In to add comment