From 0e72e00f96f3223c85408317d53faf687052b73e Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Thu, 28 Mar 2019 15:41:34 +0100
Subject: [PATCH] Fix #186

---
 .../base-param-input.component.ts             |  4 +-
 .../generic-calculator/calc-name.component.ts | 39 +++++++------------
 .../generic-input/generic-input.component.ts  |  9 +++--
 .../ngparam-input/ngparam-input.component.ts  |  4 +-
 4 files changed, 22 insertions(+), 34 deletions(-)

diff --git a/src/app/components/base-param-input/base-param-input.component.ts b/src/app/components/base-param-input/base-param-input.component.ts
index 001a186d7..9b69dd05e 100644
--- a/src/app/components/base-param-input/base-param-input.component.ts
+++ b/src/app/components/base-param-input/base-param-input.component.ts
@@ -92,8 +92,8 @@ export class NgBaseParam extends Observable {
     templateUrl: "../generic-input/generic-input.component.html",
 })
 export class BaseParamInputComponent extends GenericInputComponent {
-    constructor(private intlService: I18nService, cdRef: ChangeDetectorRef) {
-        super(cdRef);
+    constructor(intlService: I18nService, cdRef: ChangeDetectorRef) {
+        super(cdRef, intlService);
     }
 
     /**
diff --git a/src/app/components/generic-calculator/calc-name.component.ts b/src/app/components/generic-calculator/calc-name.component.ts
index 3de415d2e..309526b77 100644
--- a/src/app/components/generic-calculator/calc-name.component.ts
+++ b/src/app/components/generic-calculator/calc-name.component.ts
@@ -1,6 +1,7 @@
-import { Component, Input } from "@angular/core";
+import { Component } from "@angular/core";
 import { GenericInputComponent } from "../generic-input/generic-input.component";
 import { FormulaireDefinition } from "../../formulaire/definition/form-definition";
+import { I18nService } from "../../services/internationalisation/internationalisation.service";
 
 @Component({
     selector: "calc-name",
@@ -11,8 +12,8 @@ import { FormulaireDefinition } from "../../formulaire/definition/form-definitio
 })
 export class CalculatorNameComponent extends GenericInputComponent {
 
-    constructor() {
-        super(null);
+    constructor(intlService: I18nService) {
+        super(null, intlService);
     }
 
     /**
@@ -39,41 +40,27 @@ export class CalculatorNameComponent extends GenericInputComponent {
         this.updateAndValidateUI();
     }
 
-    /**
-     * valide une valeur de modèle : est ce une valeur acceptable ? (par ex, nombre dans un intervalle, valeur dans une liste, ...)
-     * @param v valide la valeur du modèle
-     * @returns isValid : true si la valeur est valide, false sinon
-     * @returns message : message d'erreur
-     */
     protected validateModelValue(v: any): { isValid: boolean, message: string } {
-        let msg;
-        let valid = false;
-
-        if (!(typeof (v) === "string") || v.length < 1) {
-            msg = "Veuillez entrer un nom";
-        } else {
-            valid = true;
-        }
-
-        return { isValid: valid, message: msg };
+        // no model validation for a simple string
+        return { isValid: true, message: undefined };
     }
 
-    /**
-     * valide une valeur saisie dans l'UI (forme de la saisie : est ce bien une date, un nombre, ...)
-     * @param ui valide la valeur saisie
-     * @returns isValid : true si la valeur est valide, false sinon
-     * @returns message : message d'erreur
-     */
     protected validateUIValue(ui: string): { isValid: boolean, message: string } {
         let valid = false;
         let msg: string;
 
         if (ui === undefined || ui.length < 1) {
-            msg = "Veuillez entrer un nom";
+            msg = "Veuillez entrer un nom tralala";
         } else {
             valid = true;
         }
 
         return { isValid: valid, message: msg };
     }
+
+    public updateModelFromUI() {
+        if (this.validateUI()) {
+            this.setAndValidateModel(this, this.uiValue); // do NOT cast UI value to Number
+        }
+    }
 }
diff --git a/src/app/components/generic-input/generic-input.component.ts b/src/app/components/generic-input/generic-input.component.ts
index 213dfeeaa..1bac27018 100644
--- a/src/app/components/generic-input/generic-input.component.ts
+++ b/src/app/components/generic-input/generic-input.component.ts
@@ -4,6 +4,7 @@ import { BaseComponent } from "../base/base.component";
 import { isNumeric } from "jalhyd";
 import { FormulaireDefinition } from "../../formulaire/definition/form-definition";
 import { NgParameter } from "../../formulaire/ngparam";
+import { I18nService } from "../../services/internationalisation/internationalisation.service";
 
 /**
  * classe de gestion générique d'un champ de saisie avec titre, validation et message d'erreur
@@ -88,7 +89,7 @@ export abstract class GenericInputComponent extends BaseComponent implements OnC
 
     @ViewChild("inputControl") inputField: NgModel;
 
-    constructor(private cdRef: ChangeDetectorRef) {
+    constructor(private cdRef: ChangeDetectorRef, protected intlService: I18nService) {
         super();
     }
 
@@ -129,7 +130,7 @@ export abstract class GenericInputComponent extends BaseComponent implements OnC
         }
     }
 
-    private validateUI() {
+    protected validateUI() {
         const { isValid, message } = this.validateUIValue(this._uiValue);
         this._errorMessageUI = message;
         this.detectChanges();
@@ -185,7 +186,7 @@ export abstract class GenericInputComponent extends BaseComponent implements OnC
         this.change.emit({ "action": "model", "value": this.getModelValue() });
     }
 
-    private setAndValidateModel(sender: any, v: any) {
+    protected setAndValidateModel(sender: any, v: any) {
         this.setModelValue(sender, v);
         this.emitModelChanged();
 
@@ -281,7 +282,7 @@ export abstract class GenericInputComponent extends BaseComponent implements OnC
         let msg: string;
 
         if (! isNumeric(ui)) {
-            msg = "Veuillez entrer une valeur numérique";
+            msg = this.intlService.localizeText("ERROR_PARAM_MUST_BE_A_NUMBER");
         } else {
             valid = true;
         }
diff --git a/src/app/components/ngparam-input/ngparam-input.component.ts b/src/app/components/ngparam-input/ngparam-input.component.ts
index 7e1af55ce..2717729d8 100644
--- a/src/app/components/ngparam-input/ngparam-input.component.ts
+++ b/src/app/components/ngparam-input/ngparam-input.component.ts
@@ -29,8 +29,8 @@ export class NgParamInputComponent extends GenericInputComponent implements Obse
      */
     private _tmp: number;
 
-    constructor(private intlService: I18nService, cdRef: ChangeDetectorRef) {
-        super(cdRef);
+    constructor(intlService: I18nService, cdRef: ChangeDetectorRef) {
+        super(cdRef, intlService);
     }
 
     /**
-- 
GitLab