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:
commit
f1935eb7a0
7 changed files with 16 additions and 11 deletions
|
@ -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?
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
|
@ -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 }
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue