Extend Form Validation Rule

This commit is contained in:
mauriziovitale84 2016-09-07 12:12:12 +01:00
parent 6bdc0dd054
commit f45b0b21a7
3 changed files with 36 additions and 14 deletions

View File

@ -12,6 +12,7 @@
</label> </label>
</p> </p>
</div> </div>
<alfresco-login (executeSubmit)="validateForm($event)" [providers]="providers" <alfresco-login [providers]="providers" [fieldsValidation]="customValidation"
(executeSubmit)="validateForm($event)"
(onSuccess)="onLogin($event)" (onSuccess)="onLogin($event)"
(onError)="onError($event)" #alfrescologin></alfresco-login> (onError)="onError($event)" #alfrescologin></alfresco-login>

View File

@ -15,9 +15,10 @@
* limitations under the License. * limitations under the License.
*/ */
import {Component, ViewChild} from '@angular/core'; import {Component, ViewChild, OnInit} from '@angular/core';
import {AlfrescoLoginComponent} from 'ng2-alfresco-login'; import {AlfrescoLoginComponent} from 'ng2-alfresco-login';
import {ROUTER_DIRECTIVES, Router} from '@angular/router'; import {ROUTER_DIRECTIVES, Router} from '@angular/router';
import {Validators} from '@angular/common';
declare let __moduleName: string; declare let __moduleName: string;
@ -28,14 +29,24 @@ declare let __moduleName: string;
directives: [ROUTER_DIRECTIVES, AlfrescoLoginComponent], directives: [ROUTER_DIRECTIVES, AlfrescoLoginComponent],
pipes: [] pipes: []
}) })
export class LoginDemoComponent { export class LoginDemoComponent implements OnInit {
@ViewChild('alfrescologin') @ViewChild('alfrescologin')
alfrescologin: any; alfrescologin: any;
providers: string = 'ECM'; providers: string = 'ECM';
customValidation: any;
constructor(public router: Router) { constructor(public router: Router) {
this.customValidation = {
username: ['', Validators.compose([Validators.required, Validators.minLength(8), , Validators.maxLength(10)])],
password: ['', Validators.required]
};
}
ngOnInit() {
this.alfrescologin.addCustomValidationError('username', 'minlength', 'Username must be at least 8 characters.');
this.alfrescologin.addCustomValidationError('username', 'maxlength', 'Username must not be longer than 11 characters.');
} }
onLogin($event) { onLogin($event) {
@ -74,7 +85,7 @@ export class LoginDemoComponent {
validateForm(event: any) { validateForm(event: any) {
let values = event.values; let values = event.values;
if (values.controls['username'].value === 'invalidUsername') { if (values.controls['username'].value === 'invalidUsername') {
this.alfrescologin.addCustomError('username', 'This particular username has been blocked'); this.alfrescologin.addCustomFormError('username', 'This particular username has been blocked');
event.preventDefault(); event.preventDefault();
} }
} }

View File

@ -16,7 +16,7 @@
*/ */
import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core'; import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core';
import { FORM_DIRECTIVES, ControlGroup, FormBuilder, Validators } from '@angular/common'; import { FORM_DIRECTIVES, ControlGroup, FormBuilder } from '@angular/common';
import { import {
AlfrescoTranslationService, AlfrescoTranslationService,
AlfrescoPipeTranslate, AlfrescoPipeTranslate,
@ -52,6 +52,9 @@ export class AlfrescoLoginComponent implements OnInit {
@Input() @Input()
providers: string; providers: string;
@Input()
fieldsValidation: any;
@Output() @Output()
onSuccess = new EventEmitter(); onSuccess = new EventEmitter();
@ -83,16 +86,13 @@ export class AlfrescoLoginComponent implements OnInit {
private translate: AlfrescoTranslationService) { private translate: AlfrescoTranslationService) {
translate.addTranslationFolder('node_modules/ng2-alfresco-login/dist/src'); translate.addTranslationFolder('node_modules/ng2-alfresco-login/dist/src');
this.initFormError();
this.initFormMessages();
} }
ngOnInit() { ngOnInit() {
this.initFormError(); this.form = this._fb.group(this.fieldsValidation);
this.initFormMessages();
this.form = this._fb.group({
username: ['', Validators.compose([Validators.required, Validators.minLength(4)])],
password: ['', Validators.required]
});
this.form.valueChanges.subscribe(data => this.onValueChanged(data)); this.form.valueChanges.subscribe(data => this.onValueChanged(data));
} }
@ -179,14 +179,24 @@ export class AlfrescoLoginComponent implements OnInit {
} }
/** /**
* Add a new custom error for a field * Add a custom form error for a field
* @param field * @param field
* @param msg * @param msg
*/ */
public addCustomError(field: string, msg: string) { public addCustomFormError(field: string, msg: string) {
this.formError[field] += msg; this.formError[field] += msg;
} }
/**
* Add a custom validation rule error for a field
* @param field
* @param ruleId - i.e. required | minlength | maxlength
* @param msg
*/
public addCustomValidationError(field: string, ruleId: string, msg: string) {
this._message[field][ruleId] = msg;
}
/** /**
* Display and hide the password value. * Display and hide the password value.
*/ */