From 3ec32f216ad1545b3b81d358ef547a3cc7e8ab2e Mon Sep 17 00:00:00 2001 From: mauriziovitale84 Date: Wed, 7 Sep 2016 16:14:53 +0100 Subject: [PATCH] Improve custom validation rules --- .../components/login/login-demo.component.ts | 6 ++-- .../components/alfresco-login.component.ts | 36 ++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/demo-shell-ng2/app/components/login/login-demo.component.ts b/demo-shell-ng2/app/components/login/login-demo.component.ts index a79cd90d33..53adbb504c 100644 --- a/demo-shell-ng2/app/components/login/login-demo.component.ts +++ b/demo-shell-ng2/app/components/login/login-demo.component.ts @@ -39,14 +39,16 @@ export class LoginDemoComponent implements OnInit { constructor(public router: Router) { this.customValidation = { - username: ['', Validators.compose([Validators.required, Validators.minLength(8), , Validators.maxLength(10)])], + username: ['', Validators.compose([Validators.required, Validators.minLength(4), Validators.maxLength(15)])], password: ['', Validators.required] }; } ngOnInit() { - this.alfrescologin.addCustomValidationError('username', 'minlength', 'Username must be at least 8 characters.'); + this.alfrescologin.addCustomValidationError('username', 'required', 'LOGIN.MESSAGES.USERNAME-REQUIRED'); + this.alfrescologin.addCustomValidationError('username', 'minlength', 'LOGIN.MESSAGES.USERNAME-MIN'); this.alfrescologin.addCustomValidationError('username', 'maxlength', 'Username must not be longer than 11 characters.'); + this.alfrescologin.addCustomValidationError('password', 'required', 'LOGIN.MESSAGES.PASSWORD-REQUIRED'); } onLogin($event) { diff --git a/ng2-components/ng2-alfresco-login/src/components/alfresco-login.component.ts b/ng2-components/ng2-alfresco-login/src/components/alfresco-login.component.ts index f7323ee35d..01071a1686 100644 --- a/ng2-components/ng2-alfresco-login/src/components/alfresco-login.component.ts +++ b/ng2-components/ng2-alfresco-login/src/components/alfresco-login.component.ts @@ -16,7 +16,7 @@ */ import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core'; -import { FORM_DIRECTIVES, ControlGroup, FormBuilder } from '@angular/common'; +import { FORM_DIRECTIVES, ControlGroup, FormBuilder, Validators } from '@angular/common'; import { AlfrescoTranslationService, AlfrescoPipeTranslate, @@ -88,11 +88,16 @@ export class AlfrescoLoginComponent implements OnInit { translate.addTranslationFolder('node_modules/ng2-alfresco-login/dist/src'); this.initFormError(); - this.initFormMessages(); + this.initFormFieldsMessages(); } ngOnInit() { - this.form = this._fb.group(this.fieldsValidation); + if (this.hasCustomFiledsValidation()) { + this.form = this._fb.group(this.fieldsValidation); + } else { + this.initFormFieldsDefault(); + this.initFormFieldsMessagesDefault(); + } this.form.valueChanges.subscribe(data => this.onValueChanged(data)); } @@ -232,9 +237,19 @@ export class AlfrescoLoginComponent implements OnInit { } /** - * Default form messages values + * Init form fields messages */ - private initFormMessages() { + private initFormFieldsMessages() { + this._message = { + 'username': {}, + 'password': {} + }; + } + + /** + * Default form fields messages + */ + private initFormFieldsMessagesDefault() { this._message = { 'username': { 'required': 'LOGIN.MESSAGES.USERNAME-REQUIRED', @@ -246,6 +261,13 @@ export class AlfrescoLoginComponent implements OnInit { }; } + private initFormFieldsDefault() { + this.form = this._fb.group({ + username: ['', Validators.compose([Validators.required, Validators.minLength(4)])], + password: ['', Validators.required] + }); + } + /** * Disable the error flag */ @@ -259,4 +281,8 @@ export class AlfrescoLoginComponent implements OnInit { private enableError() { this.error = true; } + + private hasCustomFiledsValidation(): boolean { + return this.fieldsValidation !== undefined; + } }