mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
Merge pull request #709 from Alfresco/dev-mvitale-692-rule
Give the possibility to customize the login form validation rules
This commit is contained in:
@@ -177,6 +177,36 @@ Alternatively you can bind to your component properties and provide values dynam
|
||||
</alfresco-login>
|
||||
```
|
||||
|
||||
#### Customize Validation rules
|
||||
If needed it is possible customize the validation rules of the login
|
||||
form. You can add/modify the default rules of the login form.
|
||||
|
||||
**MyCustomLogin.component.html**
|
||||
```html
|
||||
<alfresco-login [fieldsValidation]="customValidation"
|
||||
#alfrescologin></alfresco-login>
|
||||
```
|
||||
|
||||
**MyCustomLogin.component.ts**
|
||||
```ts
|
||||
|
||||
export class MyCustomLogin {
|
||||
customValidation: any;
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Controlling form submit execution behaviour
|
||||
|
||||
If absolutely needed it is possible taking full control over form
|
||||
@@ -204,7 +234,8 @@ export class MyCustomLogin {
|
||||
|
||||
// check if the username is in the blacklist
|
||||
if (values.controls['username'].value === 'invalidUsername') {
|
||||
this.alfrescologin.addCustomError('username', 'the username is in blacklist');
|
||||
this.alfrescologin.addCustomFormError('username', 'the
|
||||
username is in blacklist');
|
||||
event.preventDefault();
|
||||
}
|
||||
}
|
||||
|
@@ -52,6 +52,9 @@ export class AlfrescoLoginComponent implements OnInit {
|
||||
@Input()
|
||||
providers: string;
|
||||
|
||||
@Input()
|
||||
fieldsValidation: any;
|
||||
|
||||
@Output()
|
||||
onSuccess = new EventEmitter();
|
||||
|
||||
@@ -83,16 +86,18 @@ export class AlfrescoLoginComponent implements OnInit {
|
||||
private translate: AlfrescoTranslationService) {
|
||||
|
||||
translate.addTranslationFolder('node_modules/ng2-alfresco-login/dist/src');
|
||||
|
||||
this.initFormError();
|
||||
this.initFormFieldsMessages();
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
this.initFormError();
|
||||
this.initFormMessages();
|
||||
|
||||
this.form = this._fb.group({
|
||||
username: ['', Validators.compose([Validators.required, Validators.minLength(4)])],
|
||||
password: ['', Validators.required]
|
||||
});
|
||||
if (this.hasCustomFiledsValidation()) {
|
||||
this.form = this._fb.group(this.fieldsValidation);
|
||||
} else {
|
||||
this.initFormFieldsDefault();
|
||||
this.initFormFieldsMessagesDefault();
|
||||
}
|
||||
this.form.valueChanges.subscribe(data => this.onValueChanged(data));
|
||||
}
|
||||
|
||||
@@ -179,14 +184,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 msg
|
||||
*/
|
||||
public addCustomError(field: string, msg: string) {
|
||||
public addCustomFormError(field: string, msg: string) {
|
||||
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.
|
||||
*/
|
||||
@@ -222,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',
|
||||
@@ -236,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
|
||||
*/
|
||||
@@ -249,4 +281,8 @@ export class AlfrescoLoginComponent implements OnInit {
|
||||
private enableError() {
|
||||
this.error = true;
|
||||
}
|
||||
|
||||
private hasCustomFiledsValidation(): boolean {
|
||||
return this.fieldsValidation !== undefined;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user