feat(employee): Now possible to edit employee and remove data, also changed validation-rules. (TV-631)
Squashed commit of the following:
commit 082d573764be4306255d5b99f28095f96691ec43
Merge: 528b858 5846c2c
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Wed Sep 22 07:32:17 2021 +0200
Merged develop and fixed conflict
commit 528b858f17919836ce0aaa0e09e100855ad00762
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Wed Sep 22 07:25:08 2021 +0200
Updated validation
commit 28d37a1eb95a8df2520b3e7967c91e7da9e508f6
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Tue Sep 21 08:20:36 2021 +0200
Updated validation functionality to support submit without tjänster or utförande verksamheter
This commit is contained in:
@@ -34,9 +34,12 @@
|
||||
</msfa-tree-nodes-selector-panel>
|
||||
</div>
|
||||
</div>
|
||||
<ul class="tree-nodes-selector__validation-messages" *ngIf="showValidation && validationMessages">
|
||||
<li *ngFor="let validationMessage of validationMessages">
|
||||
<digi-form-validation-message af-variation="error"> {{validationMessage}} </digi-form-validation-message>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div aria-atomic="true" role="alert">
|
||||
<ul class="tree-nodes-selector__validation-messages" *ngIf="showValidation && validationMessages">
|
||||
<li *ngFor="let validationMessage of validationMessages">
|
||||
<digi-form-validation-message af-variation="error"> {{validationMessage}} </digi-form-validation-message>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
1
apps/mina-sidor-fa/src/app/shared/constants/regex.ts
Normal file
1
apps/mina-sidor-fa/src/app/shared/constants/regex.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const EMAIL_REGEX = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
|
||||
@@ -1,14 +1,17 @@
|
||||
import { AbstractControl, ValidatorFn } from '@angular/forms';
|
||||
import { EMAIL_REGEX } from '@msfa-constants/regex';
|
||||
import { ValidationError } from '@msfa-models/validation-error.model';
|
||||
|
||||
const EMAIL_REGEX = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
|
||||
export function emailIsValid(email: string): boolean {
|
||||
return EMAIL_REGEX.test(email.toLowerCase());
|
||||
}
|
||||
|
||||
export function EmailValidator(label = 'Fältet'): ValidatorFn {
|
||||
return (control: AbstractControl): ValidationError => {
|
||||
if (control && control.value) {
|
||||
const value: string = control.value as string;
|
||||
|
||||
if (!EMAIL_REGEX.test(value.toLowerCase())) {
|
||||
if (!emailIsValid(value)) {
|
||||
return { type: 'invalid', message: `Ogiltig ${label}` };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,50 @@
|
||||
import { FormGroup, ValidatorFn } from '@angular/forms';
|
||||
import { AbstractControl, ValidatorFn } from '@angular/forms';
|
||||
import { RoleEnum } from '@msfa-enums/role.enum';
|
||||
import { TreeNode } from '@msfa-shared/components/tree-nodes-selector/services/tree-nodes-selector.service';
|
||||
import { emailIsValid } from './email.validator';
|
||||
|
||||
export class EmployeeValidator {
|
||||
static HasSelectedAtLeastOneRole(roleFormControlNames: Array<string>): ValidatorFn {
|
||||
return (fg: FormGroup): { [key: string]: unknown } => {
|
||||
if (!roleFormControlNames || roleFormControlNames.length === 0) {
|
||||
return { noRoleSelected: true };
|
||||
static isEmployeeValid(hasSelectedUtforandeVerksamhetFn: (treeNode: TreeNode) => boolean): ValidatorFn {
|
||||
return (c: AbstractControl): { [key: string]: string } => {
|
||||
let errors: { [key: string]: string } = null;
|
||||
const email = c.get('email')?.value as string;
|
||||
const roles = c.get('roles')?.value as { [key: string]: boolean };
|
||||
const tjanster = c.get('tjanster')?.value as string;
|
||||
const allaUtforandeVerksamheter = c.get('allaUtforandeVerksamheter')?.value as boolean;
|
||||
const utforandeVerksamheter = c.get('utforandeVerksamheter').value as TreeNode;
|
||||
const rolesWithObligatoryFormElements = Object.entries(roles)
|
||||
.filter(([key, value]) => !!(value && key !== RoleEnum.MSFA_AuthAdmin))
|
||||
.map(([key]) => key);
|
||||
|
||||
if (!email) {
|
||||
errors = {
|
||||
...errors,
|
||||
email: 'E-postadress är obligatoriskt',
|
||||
};
|
||||
}
|
||||
if (email && !emailIsValid(email)) {
|
||||
errors = {
|
||||
...errors,
|
||||
email: 'Ogiltig e-postadress',
|
||||
};
|
||||
}
|
||||
|
||||
return roleFormControlNames.some(roleFormControlName => fg?.controls[roleFormControlName]?.value)
|
||||
? null
|
||||
: { noRoleSelected: true };
|
||||
};
|
||||
}
|
||||
|
||||
static HasSelectedAtLeastOneUtforandeVerksamhet(
|
||||
utforandeVerksamheterFormControlName: string,
|
||||
selectAllUtforandeVerksamheterFormControlName: string,
|
||||
validationFn: (treeNode: TreeNode | null | undefined) => boolean
|
||||
): ValidatorFn {
|
||||
return (fg: FormGroup): { [key: string]: unknown } => {
|
||||
if (fg?.get(selectAllUtforandeVerksamheterFormControlName)?.value) {
|
||||
return null;
|
||||
if (rolesWithObligatoryFormElements.length) {
|
||||
if (!tjanster) {
|
||||
errors = {
|
||||
...errors,
|
||||
tjanster: 'Minst en tjänst behöver väljas',
|
||||
};
|
||||
}
|
||||
if (!allaUtforandeVerksamheter && !hasSelectedUtforandeVerksamhetFn(utforandeVerksamheter)) {
|
||||
errors = {
|
||||
...errors,
|
||||
utforandeVerksamheter: 'Minst en utförande verksamhet och adress behöver väljas',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return validationFn(fg?.get(utforandeVerksamheterFormControlName)?.value)
|
||||
? null
|
||||
: { noUtforandeVerksamhetSelected: true };
|
||||
return errors;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user