diff --git a/src/app/main/ui/forms/forms.component.html b/src/app/main/ui/forms/forms.component.html index 759a7def..02d4c41b 100644 --- a/src/app/main/ui/forms/forms.component.html +++ b/src/app/main/ui/forms/forms.component.html @@ -1,7 +1,7 @@
-
+
home @@ -27,11 +27,11 @@ reactive patterns, testing, and validation.

-
+
-
Reactive Form Example
@@ -48,16 +48,12 @@ - - First Name is required! - + First Name is required! - - Last Name is required! - + Last Name is required!
@@ -68,16 +64,12 @@ - - Address is required! - + Address is required! - - Address 2 is required! - + Address 2 is required!
@@ -86,28 +78,19 @@ - - City is required! - + City is required! - - State is required! - + State is required! + formControlName="postalCode" maxlength="5" required> {{postalCode.value.length}} / 5 - - Postal Code needs to be max. {{formErrors.postalCode.maxlength.requiredLength}} characters - - - Postal Code is required! - + Postal Code is required!
@@ -135,9 +118,7 @@ Turkey - - Country is required! - + Country is required!
@@ -146,11 +127,9 @@ -
- -
Reactive Form Errors Model
- -
{{formErrors | json}}
+
+
Reactive Form Model
+
{{form.getRawValue() | json}}
@@ -171,24 +150,20 @@ - + Fill out your name -
+
- + - - First Name is required! - + First Name is required! - + - - Last Name is required! - + Last Name is required!
@@ -205,7 +180,7 @@ - + Fill out your address @@ -215,9 +190,7 @@ - - Address is required! - + Address is required!
@@ -237,37 +210,27 @@ - + Fill out your address -
+
- + - - City is required! - + City is required! - + - - State is required! - + State is required! - + + maxlength="5" required> {{postalCode.value.length}} / 5 - - Postal Code needs to be max. - {{horizontalStepperStep3Errors.postalCode.maxlength.requiredLength}} characters - - - Postal Code is required! - + Postal Code is required!
@@ -325,24 +288,20 @@ - + Fill out your name -
+
- + - - First Name is required! - + First Name is required! - + - - Last Name is required! - + Last Name is required!
@@ -359,7 +318,7 @@ - + Fill out your address @@ -369,9 +328,7 @@ - - Address is required! - + Address is required!
@@ -391,37 +348,27 @@ - + Fill out your address
- + - - City is required! - + City is required! - + - - State is required! - + State is required! - + + maxlength="5" required> {{postalCode.value.length}} / 5 - - Postal Code needs to be max. - {{verticalStepperStep3Errors.postalCode.maxlength.requiredLength}} characters - - - Postal Code is required! - + Postal Code is required!
diff --git a/src/app/main/ui/forms/forms.component.ts b/src/app/main/ui/forms/forms.component.ts index ca09a013..b69b0413 100644 --- a/src/app/main/ui/forms/forms.component.ts +++ b/src/app/main/ui/forms/forms.component.ts @@ -1,7 +1,6 @@ import { Component, OnDestroy, OnInit } from '@angular/core'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Subject } from 'rxjs'; -import { takeUntil } from 'rxjs/operators'; @Component({ selector : 'forms', @@ -11,23 +10,16 @@ import { takeUntil } from 'rxjs/operators'; export class FormsComponent implements OnInit, OnDestroy { form: FormGroup; - formErrors: any; // Horizontal Stepper horizontalStepperStep1: FormGroup; horizontalStepperStep2: FormGroup; horizontalStepperStep3: FormGroup; - horizontalStepperStep1Errors: any; - horizontalStepperStep2Errors: any; - horizontalStepperStep3Errors: any; // Vertical Stepper verticalStepperStep1: FormGroup; verticalStepperStep2: FormGroup; verticalStepperStep3: FormGroup; - verticalStepperStep1Errors: any; - verticalStepperStep2Errors: any; - verticalStepperStep3Errors: any; // Private private _unsubscribeAll: Subject; @@ -41,51 +33,6 @@ export class FormsComponent implements OnInit, OnDestroy private _formBuilder: FormBuilder ) { - // Reactive form errors - this.formErrors = { - company : {}, - firstName : {}, - lastName : {}, - address : {}, - address2 : {}, - city : {}, - state : {}, - postalCode: {}, - country : {} - }; - - // Horizontal Stepper form error - this.horizontalStepperStep1Errors = { - firstName: {}, - lastName : {} - }; - - this.horizontalStepperStep2Errors = { - address: {} - }; - - this.horizontalStepperStep3Errors = { - city : {}, - state : {}, - postalCode: {} - }; - - // Vertical Stepper form error - this.verticalStepperStep1Errors = { - firstName: {}, - lastName : {} - }; - - this.verticalStepperStep2Errors = { - address: {} - }; - - this.verticalStepperStep3Errors = { - city : {}, - state : {}, - postalCode: {} - }; - // Set the private defaults this._unsubscribeAll = new Subject(); } @@ -117,12 +64,6 @@ export class FormsComponent implements OnInit, OnDestroy country : ['', Validators.required] }); - this.form.valueChanges - .pipe(takeUntil(this._unsubscribeAll)) - .subscribe(() => { - this.onFormValuesChanged(); - }); - // Horizontal Stepper form steps this.horizontalStepperStep1 = this._formBuilder.group({ firstName: ['', Validators.required], @@ -139,24 +80,6 @@ export class FormsComponent implements OnInit, OnDestroy postalCode: ['', [Validators.required, Validators.maxLength(5)]] }); - this.horizontalStepperStep1.valueChanges - .pipe(takeUntil(this._unsubscribeAll)) - .subscribe(() => { - this.onFormValuesChanged(); - }); - - this.horizontalStepperStep2.valueChanges - .pipe(takeUntil(this._unsubscribeAll)) - .subscribe(() => { - this.onFormValuesChanged(); - }); - - this.horizontalStepperStep3.valueChanges - .pipe(takeUntil(this._unsubscribeAll)) - .subscribe(() => { - this.onFormValuesChanged(); - }); - // Vertical Stepper form stepper this.verticalStepperStep1 = this._formBuilder.group({ firstName: ['', Validators.required], @@ -172,24 +95,6 @@ export class FormsComponent implements OnInit, OnDestroy state : ['', Validators.required], postalCode: ['', [Validators.required, Validators.maxLength(5)]] }); - - this.verticalStepperStep1.valueChanges - .pipe(takeUntil(this._unsubscribeAll)) - .subscribe(() => { - this.onFormValuesChanged(); - }); - - this.verticalStepperStep2.valueChanges - .pipe(takeUntil(this._unsubscribeAll)) - .subscribe(() => { - this.onFormValuesChanged(); - }); - - this.verticalStepperStep3.valueChanges - .pipe(takeUntil(this._unsubscribeAll)) - .subscribe(() => { - this.onFormValuesChanged(); - }); } /** @@ -206,31 +111,6 @@ export class FormsComponent implements OnInit, OnDestroy // @ Public methods // ----------------------------------------------------------------------------------------------------- - /** - * On form values changed - */ - onFormValuesChanged(): void - { - for ( const field in this.formErrors ) - { - if ( !this.formErrors.hasOwnProperty(field) ) - { - continue; - } - - // Clear previous errors - this.formErrors[field] = {}; - - // Get the control - const control = this.form.get(field); - - if ( control && control.dirty && !control.valid ) - { - this.formErrors[field] = control.errors; - } - } - } - /** * Finish the horizontal stepper */