mirror of
https://github.com/richard-loafle/fuse-angular.git
synced 2025-04-04 15:41:37 +00:00
118 lines
3.2 KiB
TypeScript
118 lines
3.2 KiB
TypeScript
import { Component, OnDestroy, OnInit } from '@angular/core';
|
|
import { AbstractControl, FormBuilder, FormGroup, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
|
|
import { Subject } from 'rxjs';
|
|
import { takeUntil } from 'rxjs/operators';
|
|
|
|
import { FuseConfigService } from '@fuse/services/config.service';
|
|
import { fuseAnimations } from '@fuse/animations';
|
|
|
|
@Component({
|
|
selector : 'register-2',
|
|
templateUrl: './register-2.component.html',
|
|
styleUrls : ['./register-2.component.scss'],
|
|
animations : fuseAnimations
|
|
})
|
|
export class Register2Component implements OnInit, OnDestroy
|
|
{
|
|
registerForm: FormGroup;
|
|
|
|
// Private
|
|
private _unsubscribeAll: Subject<any>;
|
|
|
|
constructor(
|
|
private _fuseConfigService: FuseConfigService,
|
|
private _formBuilder: FormBuilder
|
|
)
|
|
{
|
|
// Configure the layout
|
|
this._fuseConfigService.config = {
|
|
layout: {
|
|
navbar : {
|
|
hidden: true
|
|
},
|
|
toolbar : {
|
|
hidden: true
|
|
},
|
|
footer : {
|
|
hidden: true
|
|
},
|
|
sidepanel: {
|
|
hidden: true
|
|
}
|
|
}
|
|
};
|
|
|
|
// Set the private defaults
|
|
this._unsubscribeAll = new Subject();
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Lifecycle hooks
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* On init
|
|
*/
|
|
ngOnInit(): void
|
|
{
|
|
this.registerForm = this._formBuilder.group({
|
|
name : ['', Validators.required],
|
|
email : ['', [Validators.required, Validators.email]],
|
|
password : ['', Validators.required],
|
|
passwordConfirm: ['', [Validators.required, confirmPasswordValidator]]
|
|
});
|
|
|
|
// Update the validity of the 'passwordConfirm' field
|
|
// when the 'password' field changes
|
|
this.registerForm.get('password').valueChanges
|
|
.pipe(takeUntil(this._unsubscribeAll))
|
|
.subscribe(() => {
|
|
this.registerForm.get('passwordConfirm').updateValueAndValidity();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* On destroy
|
|
*/
|
|
ngOnDestroy(): void
|
|
{
|
|
// Unsubscribe from all subscriptions
|
|
this._unsubscribeAll.next();
|
|
this._unsubscribeAll.complete();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Confirm password validator
|
|
*
|
|
* @param {AbstractControl} control
|
|
* @returns {ValidationErrors | null}
|
|
*/
|
|
export const confirmPasswordValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
|
|
|
|
if ( !control.parent || !control )
|
|
{
|
|
return null;
|
|
}
|
|
|
|
const password = control.parent.get('password');
|
|
const passwordConfirm = control.parent.get('passwordConfirm');
|
|
|
|
if ( !password || !passwordConfirm )
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if ( passwordConfirm.value === '' )
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if ( password.value === passwordConfirm.value )
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return {'passwordsNotMatching': true};
|
|
};
|