From 938f5043a45b16f8139edf8aad9a0d89eedd6d3f Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 8 Apr 2024 16:28:08 +0200 Subject: [PATCH 1/2] feat(champs.validation): standardize champ error messages --- app/models/champs/decimal_number_champ.rb | 4 ++-- app/models/champs/integer_number_champ.rb | 2 +- spec/models/champs/decimal_number_champ_spec.rb | 4 ++-- spec/models/champs/integer_number_champ_spec.rb | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index 704ec372e..dcd29d78d 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -6,13 +6,13 @@ class Champs::DecimalNumberChamp < Champ allow_blank: true, message: -> (object, _data) { # i18n-tasks-use t('errors.messages.not_a_float') - "« #{object.libelle} » " + object.errors.generate_message(:value, :not_a_float) + object.errors.generate_message(:value, :not_a_float) } }, numericality: { allow_nil: true, allow_blank: true, message: -> (object, _data) { - "« #{object.libelle} » " + object.errors.generate_message(:value, :not_a_number) + object.errors.generate_message(:value, :not_a_number) } }, if: -> { validate_champ_value? || validation_context == :prefill } diff --git a/app/models/champs/integer_number_champ.rb b/app/models/champs/integer_number_champ.rb index d571fdfbe..a6da1c25e 100644 --- a/app/models/champs/integer_number_champ.rb +++ b/app/models/champs/integer_number_champ.rb @@ -5,7 +5,7 @@ class Champs::IntegerNumberChamp < Champ allow_blank: true, message: -> (object, _data) { # i18n-tasks-use t('errors.messages.not_an_integer') - "« #{object.libelle} » " + object.errors.generate_message(:value, :not_an_integer) + object.errors.generate_message(:value, :not_an_integer) } }, if: -> { validate_champ_value? || validation_context == :prefill } diff --git a/spec/models/champs/decimal_number_champ_spec.rb b/spec/models/champs/decimal_number_champ_spec.rb index 18dc76d4d..39ead6506 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -20,7 +20,7 @@ describe Champs::DecimalNumberChamp do it 'is not valid and contains expected error' do expect(subject).to be_falsey - expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit comprendre maximum 3 chiffres après la virgule", "« #{champ.libelle} » n'est pas un nombre"]) + expect(champ.errors[:value]).to eq(["doit comprendre maximum 3 chiffres après la virgule", "n'est pas un nombre"]) end end @@ -29,7 +29,7 @@ describe Champs::DecimalNumberChamp do it 'is not valid and contains expected error' do expect(subject).to be_falsey - expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit comprendre maximum 3 chiffres après la virgule"]) + expect(champ.errors[:value]).to eq(["doit comprendre maximum 3 chiffres après la virgule"]) end end diff --git a/spec/models/champs/integer_number_champ_spec.rb b/spec/models/champs/integer_number_champ_spec.rb index 3eaf9bc8c..f405b1835 100644 --- a/spec/models/champs/integer_number_champ_spec.rb +++ b/spec/models/champs/integer_number_champ_spec.rb @@ -14,7 +14,7 @@ describe Champs::IntegerNumberChamp do it 'is not valid and contains errors' do is_expected.to be_falsey - expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) + expect(champ.errors[:value]).to eq(["doit être un nombre entier (sans chiffres après la virgule)"]) end end @@ -23,7 +23,7 @@ describe Champs::IntegerNumberChamp do it 'is not valid and contains errors' do is_expected.to be_falsey - expect(champ.errors[:value]).to eq(["« #{champ.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) + expect(champ.errors[:value]).to eq(["doit être un nombre entier (sans chiffres après la virgule)"]) end end From 35c92ee906ef5b730d749e7f20b656bdbd01b772 Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 8 Apr 2024 16:36:37 +0200 Subject: [PATCH 2/2] feat(champs.errors.format): better format for error below champs Co-authored-by: Corinne Durrmeyer --- app/components/dsfr/input_status_message_component.rb | 3 ++- .../input_status_message_component.html.haml | 8 ++++++-- .../editable_champ_component.html.haml | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/components/dsfr/input_status_message_component.rb b/app/components/dsfr/input_status_message_component.rb index a2b8fa510..608ea19d6 100644 --- a/app/components/dsfr/input_status_message_component.rb +++ b/app/components/dsfr/input_status_message_component.rb @@ -1,9 +1,10 @@ module Dsfr class InputStatusMessageComponent < ApplicationComponent - def initialize(errors_on_attribute:, error_full_messages:, described_by:) + def initialize(errors_on_attribute:, error_full_messages:, described_by:, champ:) @errors_on_attribute = errors_on_attribute @error_full_messages = error_full_messages @described_by = described_by + @champ = champ end def render? diff --git a/app/components/dsfr/input_status_message_component/input_status_message_component.html.haml b/app/components/dsfr/input_status_message_component/input_status_message_component.html.haml index 2fda4e477..9edef67b1 100644 --- a/app/components/dsfr/input_status_message_component/input_status_message_component.html.haml +++ b/app/components/dsfr/input_status_message_component/input_status_message_component.html.haml @@ -1,3 +1,7 @@ .fr-messages-group{ id: @describedby_id } - - @error_full_messages.each do |error_message| - %p{ class: class_names('fr-message' => true, "fr-message--#{@errors_on_attribute ? 'error' : 'valid'}" => true) }= error_message + - if @error_full_messages.size > 0 + %p{ class: class_names('fr-message' => true, "fr-message--#{@errors_on_attribute ? 'error' : 'valid'}" => true) } + = "« #{@champ.libelle} » " + + - @error_full_messages.each do |error_message| + = error_message diff --git a/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml b/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml index 7bb16ec8f..11ec41351 100644 --- a/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml +++ b/app/components/editable_champ/editable_champ_component/editable_champ_component.html.haml @@ -5,6 +5,6 @@ = render champ_component - = render Dsfr::InputStatusMessageComponent.new(errors_on_attribute: champ_component.errors_on_attribute?, error_full_messages: champ_component.error_full_messages, described_by: @champ.describedby_id) + = render Dsfr::InputStatusMessageComponent.new(errors_on_attribute: champ_component.errors_on_attribute?, error_full_messages: champ_component.error_full_messages, described_by: @champ.describedby_id, champ: @champ) = @form.hidden_field :id, value: @champ.id