debigging field validators...

This commit is contained in:
Maria Teresa Paratore 2024-06-19 17:56:59 +02:00
parent e6ccb6b646
commit 05a1d916bf
5 changed files with 95 additions and 62 deletions

View File

@ -31,19 +31,16 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -167,7 +167,7 @@ public class InformationSystemService {
switch(typeForClient) {
case "Boolean": tmp="boolean";
break;
case "Date": tmp="date";
case "Date": tmp="datetime";
break;
case "String": tmp="text";
break;
@ -198,7 +198,7 @@ public class InformationSystemService {
if(fp.isMandatory()) {
ValidationObjDTO vv = new ValidationObjDTO();
vv.setMessage("The field is required");
vv.setMessage("This field is required");
vv.setName("required");
vv.setValidator("required");
validations.add(vv);
@ -211,6 +211,13 @@ public class InformationSystemService {
vv.setValidator("required");
validations.add(vv);
}
if(fp.getPropertyType().equals("Date")) {
ValidationObjDTO vv = new ValidationObjDTO();
vv.setMessage("A valid ISO datetime is required");
vv.setName("datetime");
vv.setValidator("this.validateDatetime()");
validations.add(vv);
}
if(StringUtils.isNotEmpty(fp.getRegexp())) {
ValidationObjDTO vv = new ValidationObjDTO();

View File

@ -78,10 +78,9 @@
<ng-template #singlefield>
<mat-form-field>
<mat-label>{{prop.label}}</mat-label>
<input matInput formControlName="{{prop.name}}" id="{{prop.name}}"
type="{{prop.type}}"/>
<mat-error>{{checkForPropErrors(facetTemplate.key,ind,prop)}}</mat-error>
</mat-form-field>
<input matInput formControlName="{{prop.name}}" id="{{prop.name}}" type="{{prop.type}}"/>
</mat-form-field>
<mat-error style="display: inline-block;">{{checkForPropErrors(facetTemplate.key,ind,prop)}}</mat-error>
</ng-template>
</div>
</div>
@ -118,7 +117,7 @@
</mat-form-field >
<mat-form-field *ngIf="x.get('tipo')?.value === 'datetime'">
<mat-label for="val">value</mat-label>
<input style="width: 350px;" matInput formControlName="val" type="text" />
<input style="width: auto" matInput formControlName="val" type="text" />
<mat-error>{{checkForErrorsIn(facetTemplate.key,ind,i,'datetime')}}</mat-error>
</mat-form-field >
<mat-form-field *ngIf="x.get('tipo')?.value === 'date'">
@ -137,15 +136,6 @@
<mat-error>{{checkForErrorsIn(facetTemplate.key,ind,i,'float')}}</mat-error>
</mat-form-field >
<!--
<div *ngIf="input.type === 'number' ">
<input type="number">
</div>
<div *ngIf="inpyt.type === 'text' ">
<input type="text">
</div>
-->
<button mat-stroked-button color="primary" style="margin-left: 12px;"
(click)="removeExtraProp(facetTemplate.key,ind,i)" >
Remove custom property</button>

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-labels */
/* eslint-disable no-useless-escape */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
@ -6,7 +7,7 @@
/* eslint-disable no-console */
import { CommonModule, DatePipe } from '@angular/common';
import { Component, Inject, OnInit } from '@angular/core';
import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { AbstractControl, FormArray, FormBuilder, FormControl, FormGroup, ReactiveFormsModule, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { IContextNode } from 'app/services/i-context-node';
import { IResource } from 'app/services/i-resource';
@ -19,6 +20,7 @@ import { MatExpansionModule } from '@angular/material/expansion';
import { IFacetComposer, IFacetProps } from './i-facet-composer';
import { SharedModule } from 'app/shared/shared.module';
import { MatSelectFilterModule } from 'mat-select-filter';
import { isEmpty } from 'rxjs';
@Component({
standalone: true,
@ -133,11 +135,13 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
get val():FormControl{
return this.fb.control({
updateOn: 'change'
})
}
get props():FormGroup{
return this.fb.group({
updateOn: 'change'
})
}
@ -159,12 +163,13 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
this.getExtraPropsArray(denoFacet, indexFct).removeAt(index);
}
updateExtraPropType(denoFacet:string, indexFct:number, index:number, newValue:string):void{
this.getExtraPropsArray(denoFacet, indexFct).controls[index].get('tipo')?.setValue(newValue);
const valFc:AbstractControl = this.getExtraPropsArray(denoFacet, indexFct).controls[index].get('val')!;
if(newValue==='boolean'){
valFc.setValue('');
//valFc.addValidators(Validators.pattern('^\s*(true|false)\s*$'))
valFc.setValue('false');
// eslint-disable-next-line no-useless-escape
valFc.setValidators([Validators.required,Validators.pattern('^\s*(true|false)\s*$')]);
@ -172,8 +177,11 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
if(newValue==='datetime'){
valFc.setValue('');
valFc.setValue(new Date().toISOString());
const patt = '\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)';
valFc.setValidators([Validators.required,Validators.pattern(patt)]);
valFc.setValidators([Validators.required, this.validateDatetime()]);
}
if(newValue==='date'){
valFc.setValue('');
valFc.setValidators([Validators.required]);
}
if(newValue==='password'||newValue==='text'||newValue==='number'){
valFc.setValue('');
@ -192,7 +200,16 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
}
private validateDatetime():ValidatorFn {
// alert('prova!');
return (control: AbstractControl): ValidationErrors |null => {
const value = control.value;
const isFormallyCorrect = /^(19|20)\d{2}-(0[1-9]|1[0,1,2])-(0[1-9]|[12][0-9]|3[01])T(0[0-9]|1[0-9]|2[0-4]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])\.[0-9]{3}Z$/.test(value);
const datetimeValid = isFormallyCorrect && this.isIsoDate(control.value);
alert('datetimeValid?...'+datetimeValid);
return !datetimeValid ? {datetimeIso:true}: null;
}
}
checkForErrors(valFc:AbstractControl, inputType:string): string{
let errorMsg: string;
@ -201,37 +218,60 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
if(valFc.hasError('required')){
errorMsg = 'The field must not be empty';
}
if(valFc.hasError('pattern')){
errorMsg = ''+ valFc.value+'is not a correct value';
if(!this.checkIfEmpty(valFc.value)&& inputType==='float'){
errorMsg = 'input is not a decimal number (use . as a separator)';
}
if(!this.checkIfEmpty(valFc.value)&& inputType==='boolean'){
errorMsg = 'please enter true or false';
}
if(!this.checkIfEmpty(valFc.value)&& inputType==='datetime'){
errorMsg = 'datetime provided is not in ISO format';
}
if(!this.checkIfEmpty(valFc.value)&& inputType==='date'){
if(!this.checkIfEmpty(valFc.value)&& inputType==='datetime'){
if(!this.isIsoDate(valFc.value)){
errorMsg = 'Date is not in correct ISO format';
}
}else{
if(valFc.hasError('pattern')){
errorMsg = ''+ valFc.value+'is not a correct value';
if(!this.checkIfEmpty(valFc.value)&& inputType==='float'){
errorMsg = 'input is not a decimal number (use . as a separator)';
}
if(!this.checkIfEmpty(valFc.value)&& inputType==='boolean'){
errorMsg = 'please enter true or false';
}
if(!this.checkIfEmpty(valFc.value)&& inputType==='date'){
errorMsg = 'date or date format is not correct';
}
}
}
}
}
return errorMsg;
}
checkDatetime(denoFacet:string, indexFct:number, prop:IFacetProps):string{
let errorMsg: string;
// eslint-disable-next-line prefer-const
errorMsg='';
const valFc = this.getPropsGroup(denoFacet,indexFct).get(prop.name)!;
if(!this.isIsoDate(valFc.value)){
errorMsg = 'Date is not in correct ISO format';
}
return errorMsg;
}
isIsoDate(str:string):boolean {
const d = new Date(str);
return d instanceof Date && !isNaN(d.getTime()) && d.toISOString()===str; // valid date
}
checkForPropErrors(denoFacet:string, indexFct:number, prop:IFacetProps):string{
const abstractControl = this.getPropsGroup(denoFacet,indexFct).get(prop.name)!;
//TODO: usare questi campi per costruire la validazione:
//prop.validations
for(let i=0; i<prop.validations.length; i++){
const valObj = prop.validations[i];
valObj.message;
valObj.validator;
}
return this.checkForErrors(abstractControl,prop.type);
let errorMsg: string;
// eslint-disable-next-line prefer-const
errorMsg='';
const valFc = this.getPropsGroup(denoFacet,indexFct).get(prop.name)!;
for(let i=0; i<prop.validations.length; i++){
const valObj = prop.validations[i];
if(valFc.hasError(valObj.validator)){
console.debug()
errorMsg = valObj.message;
}
}
return errorMsg;
}
checkForErrorsIn(denoFacet:string, indexFct:number, index:number, inputType:string):string{
@ -295,8 +335,7 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
for(let i=0; i<props.length; i++){
const prop=props[i];
if(prop.type==="datetime"){
const date = new Date();
fc = this.fb.control(date);
fc = this.fb.control(new Date().toISOString);
}
if(prop.type==="number"){
fc = this.fb.control(0)
@ -314,10 +353,10 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
}
if(prop.validations[k].name==='pattern'){
fc.addValidators(Validators.pattern(prop.pattern))
//validations":[{"name":"required","validator":"required","message":"The field is required"}]
}
}
}
if(prop.type==="typeprop"){
propsFg.addControl("credentialsType",this.fb.control(''));
propsFg.addControl("user",this.fb.control(''));

View File

@ -22,5 +22,5 @@ export interface IFacetProps {
export interface IValidation {
name: string;
validator: string;
message: boolean;
message: string;
}