From 425ae8fabfdb1bbefc6203f620b6ef1e7b6f82a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois=20Grand?= <francois.grand@inrae.fr>
Date: Wed, 18 May 2022 15:19:15 +0200
Subject: [PATCH] fix: set invalid parameter single value to undefined

refs #501
---
 .../components/generic-input/generic-input.component.ts   | 3 +++
 .../components/ngparam-input/ngparam-input.component.ts   | 8 +++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/app/components/generic-input/generic-input.component.ts b/src/app/components/generic-input/generic-input.component.ts
index 3dc390d50..c30e69123 100644
--- a/src/app/components/generic-input/generic-input.component.ts
+++ b/src/app/components/generic-input/generic-input.component.ts
@@ -154,11 +154,14 @@ export abstract class GenericInputComponentDirective implements OnChanges {
             this.emitValidChanged();
         }
         // répercussion des erreurs sur le Form angular, pour faire apparaître/disparaître les mat-error
+        // setTimeout(() => {  // en cas de pb, décommenter le timeout
         if (b) {
             this.inputField.control.setErrors(null);
         } else {
             this.inputField.control.setErrors({ "incorrect": true });
         }
+        //     this.inputField.control.markAsTouched();
+        // }, 100);
     }
 
     private validateModel(): boolean {
diff --git a/src/app/components/ngparam-input/ngparam-input.component.ts b/src/app/components/ngparam-input/ngparam-input.component.ts
index 2449f691e..781e29b15 100644
--- a/src/app/components/ngparam-input/ngparam-input.component.ts
+++ b/src/app/components/ngparam-input/ngparam-input.component.ts
@@ -2,7 +2,7 @@
 
 import { Component, ChangeDetectorRef, OnDestroy, Input, ElementRef } from "@angular/core";
 
-import { Message, Observer } from "jalhyd";
+import { Message, MessageCode, Observer } from "jalhyd";
 
 import { I18nService } from "../../services/internationalisation.service";
 import { NgParameter } from "../../formulaire/elements/ngparam";
@@ -99,6 +99,9 @@ export class NgParamInputComponent extends GenericInputComponentDirective implem
             msg = "internal error, model undefined";
         } else {
             try {
+                if (!this._paramDef.allowEmpty && v === undefined) {
+                    throw new Message(MessageCode.ERROR_PARAMDEF_VALUE_UNDEFINED);
+                }
                 this._paramDef.checkValue(v);
                 valid = true;
             } catch (e) {
@@ -148,6 +151,9 @@ export class NgParamInputComponent extends GenericInputComponentDirective implem
     }
 
     public ngOnDestroy() {
+        if (!this.isValid && this.getModelValue() !== undefined) {
+            this.setModelValue(this, undefined);
+        }
         this._paramDef.removeObserver(this);
     }
 }
-- 
GitLab