Merge pull request #10318 from mfo/US/better-error-messages

ETQ Usager, je souhaite avoir des messages d'erreur homogènes et comprehensibles
This commit is contained in:
mfo 2024-04-12 11:55:39 +00:00 committed by GitHub
commit f1935eb7a0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 16 additions and 11 deletions

View file

@ -1,9 +1,10 @@
module Dsfr module Dsfr
class InputStatusMessageComponent < ApplicationComponent 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 @errors_on_attribute = errors_on_attribute
@error_full_messages = error_full_messages @error_full_messages = error_full_messages
@described_by = described_by @described_by = described_by
@champ = champ
end end
def render? def render?

View file

@ -1,3 +1,7 @@
.fr-messages-group{ id: @describedby_id } .fr-messages-group{ id: @describedby_id }
- 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_full_messages.each do |error_message|
%p{ class: class_names('fr-message' => true, "fr-message--#{@errors_on_attribute ? 'error' : 'valid'}" => true) }= error_message = error_message

View file

@ -5,6 +5,6 @@
= render champ_component = 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 = @form.hidden_field :id, value: @champ.id

View file

@ -6,13 +6,13 @@ class Champs::DecimalNumberChamp < Champ
allow_blank: true, allow_blank: true,
message: -> (object, _data) { message: -> (object, _data) {
# i18n-tasks-use t('errors.messages.not_a_float') # 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: { }, numericality: {
allow_nil: true, allow_nil: true,
allow_blank: true, allow_blank: true,
message: -> (object, _data) { 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 } }, if: -> { validate_champ_value? || validation_context == :prefill }

View file

@ -5,7 +5,7 @@ class Champs::IntegerNumberChamp < Champ
allow_blank: true, allow_blank: true,
message: -> (object, _data) { message: -> (object, _data) {
# i18n-tasks-use t('errors.messages.not_an_integer') # 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 } }, if: -> { validate_champ_value? || validation_context == :prefill }

View file

@ -20,7 +20,7 @@ describe Champs::DecimalNumberChamp do
it 'is not valid and contains expected error' do it 'is not valid and contains expected error' do
expect(subject).to be_falsey 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
end end
@ -29,7 +29,7 @@ describe Champs::DecimalNumberChamp do
it 'is not valid and contains expected error' do it 'is not valid and contains expected error' do
expect(subject).to be_falsey 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
end end

View file

@ -14,7 +14,7 @@ describe Champs::IntegerNumberChamp do
it 'is not valid and contains errors' do it 'is not valid and contains errors' do
is_expected.to be_falsey 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
end end
@ -23,7 +23,7 @@ describe Champs::IntegerNumberChamp do
it 'is not valid and contains errors' do it 'is not valid and contains errors' do
is_expected.to be_falsey 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
end end