From d1cb5162220567998f5d357346f754c257111ca6 Mon Sep 17 00:00:00 2001 From: Nicolas Senave Date: Thu, 5 Dec 2024 14:32:16 +0100 Subject: [PATCH] feat: add 'at least 4 digits' in control --- build.gradle.kts | 2 +- .../out/steps/lunatic/LunaticAddControlFormat.java | 9 ++++++--- .../out/steps/lunatic/LunaticAddControlFormatTest.java | 5 +++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 34889788c..51772d668 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -16,7 +16,7 @@ java { allprojects { group = "fr.insee.eno" - version = "3.30.0-SNAPSHOT" + version = "3.30.0-SNAPSHOT.1" } subprojects { diff --git a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormat.java b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormat.java index d76b99c7c..5421c1dfb 100644 --- a/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormat.java +++ b/eno-core/src/main/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormat.java @@ -154,6 +154,8 @@ private void createFormatControlsForDatepicker(Datepicker datepicker) { Optional controlBounds = getFormatControlFromDatepickerAttributes(id, minValue, maxValue, format, responseName); controlBounds.ifPresent(controls::addFirst); controlYearFormat.ifPresent(controls::addFirst); + // Note: it's important that the year format is added in first position, since in some cases only the message + // of the first control is displayed. } /** @@ -198,9 +200,10 @@ private Optional generateDatepickerYearControl(String id, String fo return Optional.empty(); } String controlId = id + "-format-year"; - String expression = String.format( - "not(not(isnull(%s)) and cast(cast(cast(%s, date, \"%s\"), string, \"YYYY\"), integer) <= 999)", - responseName, responseName, format); + String expression = String.format("not(not(isnull(%s)) and (" + + "cast(cast(cast(%s, date, \"%s\"), string, \"YYYY\"), integer) <= 999 or " + + "cast(cast(cast(%s, date, \"%s\"), string, \"YYYY\"), integer) > 9999))", + responseName, responseName, format, responseName, format); String message = "\"L'année doit être saisie avec 4 chiffres.\""; return Optional.of(createFormatControl(controlId, expression, message)); } diff --git a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormatTest.java b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormatTest.java index 290f55295..fdb6e3405 100644 --- a/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormatTest.java +++ b/eno-core/src/test/java/fr/insee/eno/core/processing/out/steps/lunatic/LunaticAddControlFormatTest.java @@ -165,8 +165,9 @@ void datepickerYearFormatControl() { ControlType yearControl = controls.getFirst(); assertEquals("datepicker-id-format-year", yearControl.getId()); - String expected = "not(not(isnull(DATE_VAR)) " + - "and cast(cast(cast(DATE_VAR, date, \"YYYY-MM-DD\"), string, \"YYYY\"), integer) <= 999)"; + String expected = "not(not(isnull(DATE_VAR)) and (" + + "cast(cast(cast(DATE_VAR, date, \"YYYY-MM-DD\"), string, \"YYYY\"), integer) <= 999 or " + + "cast(cast(cast(DATE_VAR, date, \"YYYY-MM-DD\"), string, \"YYYY\"), integer) > 9999))"; assertEquals(expected, yearControl.getControl().getValue()); assertEquals(LabelTypeEnum.VTL, yearControl.getControl().getType()); assertEquals(LabelTypeEnum.VTL_MD, yearControl.getErrorMessage().getType());