Implemented better validation inside avvikelse-report
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export const EMAIL_REGEX = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;
|
||||
export const ISO_DATE_NO_TIME = /^\d{4}[-/\s]?((((0[13578])|(1[02]))[-/\s]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[-/\s]?(([0-2][0-9])|(30)))|(02[-/\s]?[0-2][0-9]))$/;
|
||||
export const CHARACTER_REGEX = /\w/;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { AbstractControl, FormArray, FormControl, FormGroup } from '@angular/forms';
|
||||
|
||||
export function markControlsAsDirty(controls: AbstractControl[]): void {
|
||||
controls.forEach(control => {
|
||||
if (control instanceof FormControl) {
|
||||
control.markAsDirty({ onlySelf: true });
|
||||
} else if (control instanceof FormGroup) {
|
||||
markControlsAsDirty(Object.values(control.controls));
|
||||
} else if (control instanceof FormArray) {
|
||||
markControlsAsDirty(control.controls);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { AbstractControl, ValidatorFn } from '@angular/forms';
|
||||
import { CHARACTER_REGEX } from '@msfa-constants/regex';
|
||||
import { ValidationError } from '@msfa-models/validation-error.model';
|
||||
|
||||
export function RegexValidator(regex = CHARACTER_REGEX, message = 'Ogiltig värde'): ValidatorFn {
|
||||
return (control: AbstractControl): ValidationError => {
|
||||
if (control && control.value) {
|
||||
if (!regex.test(control.value)) {
|
||||
return { invalid: message };
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user