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"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </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"> <classpathentry kind="src" path="target/generated-sources/annotations">
<attributes> <attributes>
<attribute name="optional" value="true"/> <attribute name="optional" value="true"/>
</attributes> </attributes>
</classpathentry> </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"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View File

@ -167,7 +167,7 @@ public class InformationSystemService {
switch(typeForClient) { switch(typeForClient) {
case "Boolean": tmp="boolean"; case "Boolean": tmp="boolean";
break; break;
case "Date": tmp="date"; case "Date": tmp="datetime";
break; break;
case "String": tmp="text"; case "String": tmp="text";
break; break;
@ -198,7 +198,7 @@ public class InformationSystemService {
if(fp.isMandatory()) { if(fp.isMandatory()) {
ValidationObjDTO vv = new ValidationObjDTO(); ValidationObjDTO vv = new ValidationObjDTO();
vv.setMessage("The field is required"); vv.setMessage("This field is required");
vv.setName("required"); vv.setName("required");
vv.setValidator("required"); vv.setValidator("required");
validations.add(vv); validations.add(vv);
@ -211,6 +211,13 @@ public class InformationSystemService {
vv.setValidator("required"); vv.setValidator("required");
validations.add(vv); 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())) { if(StringUtils.isNotEmpty(fp.getRegexp())) {
ValidationObjDTO vv = new ValidationObjDTO(); ValidationObjDTO vv = new ValidationObjDTO();

View File

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

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-labels */ /* eslint-disable no-labels */
/* eslint-disable no-useless-escape */ /* eslint-disable no-useless-escape */
/* eslint-disable @typescript-eslint/restrict-plus-operands */ /* eslint-disable @typescript-eslint/restrict-plus-operands */
@ -6,7 +7,7 @@
/* eslint-disable no-console */ /* eslint-disable no-console */
import { CommonModule, DatePipe } from '@angular/common'; import { CommonModule, DatePipe } from '@angular/common';
import { Component, Inject, OnInit } from '@angular/core'; 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 { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
import { IContextNode } from 'app/services/i-context-node'; import { IContextNode } from 'app/services/i-context-node';
import { IResource } from 'app/services/i-resource'; 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 { IFacetComposer, IFacetProps } from './i-facet-composer';
import { SharedModule } from 'app/shared/shared.module'; import { SharedModule } from 'app/shared/shared.module';
import { MatSelectFilterModule } from 'mat-select-filter'; import { MatSelectFilterModule } from 'mat-select-filter';
import { isEmpty } from 'rxjs';
@Component({ @Component({
standalone: true, standalone: true,
@ -129,15 +131,17 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
tipo:[''] tipo:['']
}) })
} }
get val():FormControl{ get val():FormControl{
return this.fb.control({ return this.fb.control({
updateOn: 'change'
}) })
} }
get props():FormGroup{ get props():FormGroup{
return this.fb.group({ 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); this.getExtraPropsArray(denoFacet, indexFct).removeAt(index);
} }
updateExtraPropType(denoFacet:string, indexFct:number, index:number, newValue:string):void{ updateExtraPropType(denoFacet:string, indexFct:number, index:number, newValue:string):void{
this.getExtraPropsArray(denoFacet, indexFct).controls[index].get('tipo')?.setValue(newValue); this.getExtraPropsArray(denoFacet, indexFct).controls[index].get('tipo')?.setValue(newValue);
const valFc:AbstractControl = this.getExtraPropsArray(denoFacet, indexFct).controls[index].get('val')!; const valFc:AbstractControl = this.getExtraPropsArray(denoFacet, indexFct).controls[index].get('val')!;
if(newValue==='boolean'){ if(newValue==='boolean'){
valFc.setValue(''); valFc.setValue('');
//valFc.addValidators(Validators.pattern('^\s*(true|false)\s*$'))
valFc.setValue('false'); valFc.setValue('false');
// eslint-disable-next-line no-useless-escape // eslint-disable-next-line no-useless-escape
valFc.setValidators([Validators.required,Validators.pattern('^\s*(true|false)\s*$')]); 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'){ if(newValue==='datetime'){
valFc.setValue(''); valFc.setValue('');
valFc.setValue(new Date().toISOString()); 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, this.validateDatetime()]);
valFc.setValidators([Validators.required,Validators.pattern(patt)]); }
if(newValue==='date'){
valFc.setValue('');
valFc.setValidators([Validators.required]);
} }
if(newValue==='password'||newValue==='text'||newValue==='number'){ if(newValue==='password'||newValue==='text'||newValue==='number'){
valFc.setValue(''); 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{ checkForErrors(valFc:AbstractControl, inputType:string): string{
let errorMsg: string; let errorMsg: string;
@ -201,37 +218,60 @@ defaultValue={new Date().toISOString().substring(0, (new Date().toISOString().in
if(valFc.hasError('required')){ if(valFc.hasError('required')){
errorMsg = 'The field must not be empty'; errorMsg = 'The field must not be empty';
} }
if(valFc.hasError('pattern')){ if(!this.checkIfEmpty(valFc.value)&& inputType==='datetime'){
errorMsg = ''+ valFc.value+'is not a correct value'; if(!this.isIsoDate(valFc.value)){
if(!this.checkIfEmpty(valFc.value)&& inputType==='float'){ errorMsg = 'Date is not in correct ISO format';
errorMsg = 'input is not a decimal number (use . as a separator)'; }
} }else{
if(!this.checkIfEmpty(valFc.value)&& inputType==='boolean'){ if(valFc.hasError('pattern')){
errorMsg = 'please enter true or false'; errorMsg = ''+ valFc.value+'is not a correct value';
} if(!this.checkIfEmpty(valFc.value)&& inputType==='float'){
if(!this.checkIfEmpty(valFc.value)&& inputType==='datetime'){ errorMsg = 'input is not a decimal number (use . as a separator)';
errorMsg = 'datetime provided is not in ISO format'; }
} if(!this.checkIfEmpty(valFc.value)&& inputType==='boolean'){
if(!this.checkIfEmpty(valFc.value)&& inputType==='date'){ errorMsg = 'please enter true or false';
}
if(!this.checkIfEmpty(valFc.value)&& inputType==='date'){
errorMsg = 'date or date format is not correct'; errorMsg = 'date or date format is not correct';
} }
}
} }
return errorMsg; 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{ checkForPropErrors(denoFacet:string, indexFct:number, prop:IFacetProps):string{
const abstractControl = this.getPropsGroup(denoFacet,indexFct).get(prop.name)!; let errorMsg: string;
//TODO: usare questi campi per costruire la validazione: // eslint-disable-next-line prefer-const
//prop.validations errorMsg='';
for(let i=0; i<prop.validations.length; i++){ const valFc = this.getPropsGroup(denoFacet,indexFct).get(prop.name)!;
const valObj = prop.validations[i]; for(let i=0; i<prop.validations.length; i++){
valObj.message; const valObj = prop.validations[i];
valObj.validator; if(valFc.hasError(valObj.validator)){
} console.debug()
errorMsg = valObj.message;
}
return this.checkForErrors(abstractControl,prop.type); }
return errorMsg;
} }
checkForErrorsIn(denoFacet:string, indexFct:number, index:number, inputType:string):string{ 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++){ for(let i=0; i<props.length; i++){
const prop=props[i]; const prop=props[i];
if(prop.type==="datetime"){ if(prop.type==="datetime"){
const date = new Date(); fc = this.fb.control(new Date().toISOString);
fc = this.fb.control(date);
} }
if(prop.type==="number"){ if(prop.type==="number"){
fc = this.fb.control(0) 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'){ if(prop.validations[k].name==='pattern'){
fc.addValidators(Validators.pattern(prop.pattern)) fc.addValidators(Validators.pattern(prop.pattern))
//validations":[{"name":"required","validator":"required","message":"The field is required"}]
} }
} }
} }
if(prop.type==="typeprop"){ if(prop.type==="typeprop"){
propsFg.addControl("credentialsType",this.fb.control('')); propsFg.addControl("credentialsType",this.fb.control(''));
propsFg.addControl("user",this.fb.control('')); propsFg.addControl("user",this.fb.control(''));

View File

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