mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-31 17:38:48 +00:00
Extend the validation form
This commit is contained in:
@@ -15,14 +15,15 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {Component, Input, Output, EventEmitter} 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, Validators } from '@angular/common';
|
||||||
import {
|
import {
|
||||||
AlfrescoTranslationService,
|
AlfrescoTranslationService,
|
||||||
AlfrescoPipeTranslate,
|
AlfrescoPipeTranslate,
|
||||||
AlfrescoAuthenticationService,
|
AlfrescoAuthenticationService,
|
||||||
AlfrescoSettingsService
|
AlfrescoSettingsService
|
||||||
} from 'ng2-alfresco-core';
|
} from 'ng2-alfresco-core';
|
||||||
|
import { FormOutcomeEvent } from '../models/form-outcome-event.model';
|
||||||
|
|
||||||
declare let componentHandler: any;
|
declare let componentHandler: any;
|
||||||
declare let __moduleName: string;
|
declare let __moduleName: string;
|
||||||
@@ -36,7 +37,7 @@ declare let __moduleName: string;
|
|||||||
pipes: [AlfrescoPipeTranslate]
|
pipes: [AlfrescoPipeTranslate]
|
||||||
|
|
||||||
})
|
})
|
||||||
export class AlfrescoLoginComponent {
|
export class AlfrescoLoginComponent implements OnInit {
|
||||||
|
|
||||||
baseComponentPath = __moduleName.replace('/alfresco-login.component.js', '');
|
baseComponentPath = __moduleName.replace('/alfresco-login.component.js', '');
|
||||||
|
|
||||||
@@ -49,7 +50,7 @@ export class AlfrescoLoginComponent {
|
|||||||
backgroundImageUrl: string;
|
backgroundImageUrl: string;
|
||||||
|
|
||||||
@Input()
|
@Input()
|
||||||
providers: string ;
|
providers: string;
|
||||||
|
|
||||||
@Output()
|
@Output()
|
||||||
onSuccess = new EventEmitter();
|
onSuccess = new EventEmitter();
|
||||||
@@ -57,6 +58,9 @@ export class AlfrescoLoginComponent {
|
|||||||
@Output()
|
@Output()
|
||||||
onError = new EventEmitter();
|
onError = new EventEmitter();
|
||||||
|
|
||||||
|
@Output()
|
||||||
|
executeOutcome: EventEmitter<FormOutcomeEvent> = new EventEmitter<FormOutcomeEvent>();
|
||||||
|
|
||||||
form: ControlGroup;
|
form: ControlGroup;
|
||||||
error: boolean = false;
|
error: boolean = false;
|
||||||
errorMsg: string;
|
errorMsg: string;
|
||||||
@@ -78,68 +82,41 @@ export class AlfrescoLoginComponent {
|
|||||||
public settingsService: AlfrescoSettingsService,
|
public settingsService: AlfrescoSettingsService,
|
||||||
private translate: AlfrescoTranslationService) {
|
private translate: AlfrescoTranslationService) {
|
||||||
|
|
||||||
this.formError = {
|
translate.addTranslationFolder('node_modules/ng2-alfresco-login/dist/src');
|
||||||
'username': '',
|
}
|
||||||
'password': ''
|
|
||||||
};
|
ngOnInit() {
|
||||||
|
this.initFormError();
|
||||||
|
this.initFormMessages();
|
||||||
|
|
||||||
this.form = this._fb.group({
|
this.form = this._fb.group({
|
||||||
username: ['', Validators.compose([Validators.required, Validators.minLength(4)])],
|
username: ['', Validators.compose([Validators.required, Validators.minLength(4)])],
|
||||||
password: ['', Validators.required]
|
password: ['', Validators.required]
|
||||||
});
|
});
|
||||||
|
|
||||||
this._message = {
|
|
||||||
'username': {
|
|
||||||
'required': 'LOGIN.MESSAGES.USERNAME-REQUIRED',
|
|
||||||
'minlength': 'LOGIN.MESSAGES.USERNAME-MIN'
|
|
||||||
},
|
|
||||||
'password': {
|
|
||||||
'required': 'LOGIN.MESSAGES.PASSWORD-REQUIRED'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
translate.addTranslationFolder('node_modules/ng2-alfresco-login/dist/src');
|
|
||||||
|
|
||||||
this.form.valueChanges.subscribe(data => this.onValueChanged(data));
|
this.form.valueChanges.subscribe(data => this.onValueChanged(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method called on submit form
|
* Method called on submit form
|
||||||
* @param value
|
* @param values
|
||||||
* @param event
|
* @param event
|
||||||
*/
|
*/
|
||||||
onSubmit(value: any, event: any) {
|
onSubmit(values: any) {
|
||||||
if (event) {
|
if (!this.checkRequiredParams()) {
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
||||||
if (this.providers === undefined) {
|
|
||||||
this.errorMsg = 'LOGIN.MESSAGES.LOGIN-ERROR-PROVIDERS';
|
|
||||||
this.error = true;
|
|
||||||
let messageProviders: any;
|
|
||||||
messageProviders = this.translate.get(this.errorMsg);
|
|
||||||
this.onError.emit(messageProviders.value);
|
|
||||||
this.success = false;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
this.error = false;
|
|
||||||
|
|
||||||
this.settingsService.setProviders(this.providers);
|
this.settingsService.setProviders(this.providers);
|
||||||
|
|
||||||
this.authService.login(value.username, value.password)
|
this.disableError();
|
||||||
.subscribe(
|
|
||||||
(token: any) => {
|
let args = new FormOutcomeEvent(this.form);
|
||||||
this.success = true;
|
this.executeOutcome.emit(args);
|
||||||
this.onSuccess.emit(token);
|
|
||||||
},
|
if (args.defaultPrevented) {
|
||||||
(err: any) => {
|
return false;
|
||||||
this.error = true;
|
} else {
|
||||||
this.errorMsg = 'LOGIN.MESSAGES.LOGIN-ERROR-CREDENTIALS';
|
this.performeLogin(values);
|
||||||
this.onError.emit(err);
|
}
|
||||||
console.log(err);
|
|
||||||
this.success = false;
|
|
||||||
},
|
|
||||||
() => console.log('Login done')
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -147,7 +124,7 @@ export class AlfrescoLoginComponent {
|
|||||||
* @param data
|
* @param data
|
||||||
*/
|
*/
|
||||||
onValueChanged(data: any) {
|
onValueChanged(data: any) {
|
||||||
this.error = false;
|
this.disableError();
|
||||||
for (let field in this.formError) {
|
for (let field in this.formError) {
|
||||||
if (field) {
|
if (field) {
|
||||||
this.formError[field] = '';
|
this.formError[field] = '';
|
||||||
@@ -164,6 +141,52 @@ export class AlfrescoLoginComponent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performe the login service
|
||||||
|
* @param values
|
||||||
|
*/
|
||||||
|
private performeLogin(values: any) {
|
||||||
|
this.authService.login(values.username, values.password)
|
||||||
|
.subscribe(
|
||||||
|
(token: any) => {
|
||||||
|
this.success = true;
|
||||||
|
this.onSuccess.emit(token);
|
||||||
|
},
|
||||||
|
(err: any) => {
|
||||||
|
this.enableError();
|
||||||
|
this.errorMsg = 'LOGIN.MESSAGES.LOGIN-ERROR-CREDENTIALS';
|
||||||
|
this.onError.emit(err);
|
||||||
|
console.log(err);
|
||||||
|
},
|
||||||
|
() => console.log('Login done')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check the require parameter
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
private checkRequiredParams(): boolean {
|
||||||
|
if (this.providers === undefined) {
|
||||||
|
this.errorMsg = 'LOGIN.MESSAGES.LOGIN-ERROR-PROVIDERS';
|
||||||
|
this.enableError();
|
||||||
|
let messageProviders: any;
|
||||||
|
messageProviders = this.translate.get(this.errorMsg);
|
||||||
|
this.onError.emit(messageProviders.value);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new custom error for a field
|
||||||
|
* @param field
|
||||||
|
* @param msg
|
||||||
|
*/
|
||||||
|
public addCustomError(field: string, msg: string) {
|
||||||
|
this.formError[field] += msg;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Display and hide the password value.
|
* Display and hide the password value.
|
||||||
*/
|
*/
|
||||||
@@ -187,4 +210,43 @@ export class AlfrescoLoginComponent {
|
|||||||
}
|
}
|
||||||
return !field.valid && field.dirty && !field.pristine;
|
return !field.valid && field.dirty && !field.pristine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default formError values
|
||||||
|
*/
|
||||||
|
private initFormError() {
|
||||||
|
this.formError = {
|
||||||
|
'username': '',
|
||||||
|
'password': ''
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default form messages values
|
||||||
|
*/
|
||||||
|
private initFormMessages() {
|
||||||
|
this._message = {
|
||||||
|
'username': {
|
||||||
|
'required': 'LOGIN.MESSAGES.USERNAME-REQUIRED',
|
||||||
|
'minlength': 'LOGIN.MESSAGES.USERNAME-MIN'
|
||||||
|
},
|
||||||
|
'password': {
|
||||||
|
'required': 'LOGIN.MESSAGES.PASSWORD-REQUIRED'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable the error flag
|
||||||
|
*/
|
||||||
|
private disableError() {
|
||||||
|
this.error = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable the error flag
|
||||||
|
*/
|
||||||
|
private enableError() {
|
||||||
|
this.error = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,39 @@
|
|||||||
|
/*!
|
||||||
|
* @license
|
||||||
|
* Copyright 2016 Alfresco Software, Ltd.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export class FormOutcomeEvent {
|
||||||
|
|
||||||
|
private _values: any;
|
||||||
|
private _defaultPrevented: boolean = false;
|
||||||
|
|
||||||
|
get values(): any {
|
||||||
|
return this._values;
|
||||||
|
}
|
||||||
|
|
||||||
|
get defaultPrevented() {
|
||||||
|
return this._defaultPrevented;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(_values: any) {
|
||||||
|
this._values = _values;
|
||||||
|
}
|
||||||
|
|
||||||
|
preventDefault() {
|
||||||
|
this._defaultPrevented = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user