mirror of
https://github.com/richard-loafle/fuse-angular.git
synced 2025-01-10 04:25:08 +00:00
(Coming Soon) Simplified the form error messages
This commit is contained in:
parent
98297ea787
commit
63bd95ea1e
|
@ -28,10 +28,11 @@
|
||||||
<mat-form-field appearance="outline">
|
<mat-form-field appearance="outline">
|
||||||
<mat-label>Email</mat-label>
|
<mat-label>Email</mat-label>
|
||||||
<input matInput formControlName="email">
|
<input matInput formControlName="email">
|
||||||
<mat-error *ngIf="comingSoonFormErrors.email.required">
|
<mat-error *ngIf="comingSoonForm.get('email').hasError('required')">
|
||||||
Email is required
|
Email is required
|
||||||
</mat-error>
|
</mat-error>
|
||||||
<mat-error *ngIf="!comingSoonFormErrors.email.required && comingSoonFormErrors.email.email">
|
<mat-error *ngIf="!comingSoonForm.get('email').hasError('required') &&
|
||||||
|
comingSoonForm.get('email').hasError('email')">
|
||||||
Please enter a valid email address
|
Please enter a valid email address
|
||||||
</mat-error>
|
</mat-error>
|
||||||
</mat-form-field>
|
</mat-form-field>
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import { Component, OnDestroy, OnInit, ViewEncapsulation } from '@angular/core';
|
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
|
||||||
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
|
||||||
import { Subject } from 'rxjs';
|
|
||||||
import { takeUntil } from 'rxjs/operators';
|
|
||||||
|
|
||||||
import { FuseConfigService } from '@fuse/services/config.service';
|
import { FuseConfigService } from '@fuse/services/config.service';
|
||||||
import { fuseAnimations } from '@fuse/animations';
|
import { fuseAnimations } from '@fuse/animations';
|
||||||
|
@ -13,13 +11,9 @@ import { fuseAnimations } from '@fuse/animations';
|
||||||
encapsulation: ViewEncapsulation.None,
|
encapsulation: ViewEncapsulation.None,
|
||||||
animations : fuseAnimations
|
animations : fuseAnimations
|
||||||
})
|
})
|
||||||
export class ComingSoonComponent implements OnInit, OnDestroy
|
export class ComingSoonComponent implements OnInit
|
||||||
{
|
{
|
||||||
comingSoonForm: FormGroup;
|
comingSoonForm: FormGroup;
|
||||||
comingSoonFormErrors: any;
|
|
||||||
|
|
||||||
// Private
|
|
||||||
private _unsubscribeAll: Subject<any>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
@ -49,14 +43,6 @@ export class ComingSoonComponent implements OnInit, OnDestroy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set the defaults
|
|
||||||
this.comingSoonFormErrors = {
|
|
||||||
email: {}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Set the private defaults
|
|
||||||
this._unsubscribeAll = new Subject();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
@ -71,50 +57,5 @@ export class ComingSoonComponent implements OnInit, OnDestroy
|
||||||
this.comingSoonForm = this._formBuilder.group({
|
this.comingSoonForm = this._formBuilder.group({
|
||||||
email: ['', [Validators.required, Validators.email]]
|
email: ['', [Validators.required, Validators.email]]
|
||||||
});
|
});
|
||||||
|
|
||||||
this.comingSoonForm.valueChanges
|
|
||||||
.pipe(takeUntil(this._unsubscribeAll))
|
|
||||||
.subscribe(() => {
|
|
||||||
this.onRegisterFormValuesChanged();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* On destroy
|
|
||||||
*/
|
|
||||||
ngOnDestroy(): void
|
|
||||||
{
|
|
||||||
// Unsubscribe from all subscriptions
|
|
||||||
this._unsubscribeAll.next();
|
|
||||||
this._unsubscribeAll.complete();
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------------------------------
|
|
||||||
// @ Public methods
|
|
||||||
// -----------------------------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
|
||||||
* On form values changed
|
|
||||||
*/
|
|
||||||
onRegisterFormValuesChanged(): void
|
|
||||||
{
|
|
||||||
for ( const field in this.comingSoonFormErrors )
|
|
||||||
{
|
|
||||||
if ( !this.comingSoonFormErrors.hasOwnProperty(field) )
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clear previous errors
|
|
||||||
this.comingSoonFormErrors[field] = {};
|
|
||||||
|
|
||||||
// Get the control
|
|
||||||
const control = this.comingSoonForm.get(field);
|
|
||||||
|
|
||||||
if ( control && control.dirty && !control.valid )
|
|
||||||
{
|
|
||||||
this.comingSoonFormErrors[field] = control.errors;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user