Validates creating a new user

- If a new user is created and neither the username, first name nor last name is set, a hint is displayed and the user is still in the creating mode.
- Also prevents that the client queries a user that does not exist.
This commit is contained in:
GabrielMeyer 2019-04-09 16:45:50 +02:00
parent a831ccd2b2
commit b7799c2337
2 changed files with 69 additions and 21 deletions

View File

@ -0,0 +1,27 @@
import { ValidationErrors, AbstractControl } from '@angular/forms';
/**
* Custom validator class
* Several strings can be passed as `keys` of the form group.
* This validator ensures that at least one of the fields behind the given keys has a value.
*/
export class OneOfValidator {
/**
* Function to confirm that at least one of the given form controls has a valid value.
*
* @param keys Undefined amount of form control names.
*
* @returns An error if all of these form controls have no valid values or null if at least one is filled.
*/
public static validation(...keys: string[]): (control: AbstractControl) => ValidationErrors | null {
return (control: AbstractControl): ValidationErrors | null => {
const formControls = keys.map(key => control.get(key));
const noOneSet = formControls.every(
formControl => (formControl && formControl.value === '') || !formControl
);
return noOneSet ? { noOneSet: true } : null;
};
}
}

View File

@ -15,6 +15,7 @@ import { UserRepositoryService } from 'app/core/repositories/users/user-reposito
import { ViewUser } from '../../models/view-user'; import { ViewUser } from '../../models/view-user';
import { ViewGroup } from '../../models/view-group'; import { ViewGroup } from '../../models/view-group';
import { GroupRepositoryService } from 'app/core/repositories/users/group-repository.service'; import { GroupRepositoryService } from 'app/core/repositories/users/group-repository.service';
import { OneOfValidator } from 'app/shared/validators/one-of-validator';
/** /**
* Users detail component for both new and existing users * Users detail component for both new and existing users
@ -226,24 +227,29 @@ export class UserDetailComponent extends BaseViewComponent implements OnInit {
* initialize the form with default values * initialize the form with default values
*/ */
public createForm(): void { public createForm(): void {
this.personalInfoForm = this.formBuilder.group({ this.personalInfoForm = this.formBuilder.group(
username: [''], {
title: [''], username: [''],
first_name: [''], title: [''],
last_name: [''], first_name: [''],
gender: [''], last_name: [''],
structure_level: [''], gender: [''],
number: [''], structure_level: [''],
about_me: [''], number: [''],
groups_id: [''], about_me: [''],
is_present: [true], groups_id: [''],
is_committee: [false], is_present: [true],
email: ['', Validators.email], is_committee: [false],
last_email_send: [''], email: ['', Validators.email],
comment: [''], last_email_send: [''],
is_active: [true], comment: [''],
default_password: [''] is_active: [true],
}); default_password: ['']
},
{
validators: OneOfValidator.validation('username', 'first_name', 'last_name')
}
);
// patch the form only for existing users // patch the form only for existing users
if (!this.newUser) { if (!this.newUser) {
@ -342,11 +348,26 @@ export class UserDetailComponent extends BaseViewComponent implements OnInit {
* Save / Submit a user * Save / Submit a user
*/ */
public async saveUser(): Promise<void> { public async saveUser(): Promise<void> {
if (this.personalInfoForm.invalid) {
let hint = '';
if (this.personalInfoForm.errors) {
hint = 'At least one of username, first name or last name has to be set.';
} else {
for (const formControl in this.personalInfoForm.controls) {
if (this.personalInfoForm.get(formControl).errors) {
hint = formControl + ' is incorrect.';
}
}
}
this.raiseError(this.translate.instant(hint));
return;
}
try { try {
if (this.newUser) { if (this.newUser) {
this.newUser = false; await this.repo.create(this.personalInfoForm.value).then(() => {
await this.repo.create(this.personalInfoForm.value); this.newUser = false;
this.router.navigate([`./users/`]); this.router.navigate([`./users/`]);
}, this.raiseError);
} else { } else {
// TODO (Issue #3962): We need a waiting-State, so if autoupdates come before the response, // TODO (Issue #3962): We need a waiting-State, so if autoupdates come before the response,
// the user is also updated. // the user is also updated.