From 56c846900bda6d6983f07e7d400575e354da8dcb Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 10 Jul 2019 11:24:57 +0000 Subject: [PATCH 1/2] champs: improve validation message of numeric fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces > Champs value doit être un nombre by > La valeur du champ doit être un nombre entier (sans chiffres après > la virgule) --- config/locales/fr.yml | 1 + config/locales/models/champs/fr.yml | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 config/locales/models/champs/fr.yml diff --git a/config/locales/fr.yml b/config/locales/fr.yml index a06351dbb..0033b1135 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -128,6 +128,7 @@ fr: messages: blank: "doit être rempli" not_a_number: 'doit être un nombre' + not_an_integer: 'doit être un nombre entier (sans chiffres après la virgule)' greater_than: "doit être supérieur à %{count}" greater_than_or_equal_to: "doit être supérieur ou égal à %{count}" less_than: "doit être inférieur à %{count}" diff --git a/config/locales/models/champs/fr.yml b/config/locales/models/champs/fr.yml new file mode 100644 index 000000000..d1ab85d27 --- /dev/null +++ b/config/locales/models/champs/fr.yml @@ -0,0 +1,5 @@ +fr: + activerecord: + attributes: + champs: + value: La valeur du champ From 10065df8cee28866d17cc7ea8a5847757c671894 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 10 Jul 2019 11:27:36 +0000 Subject: [PATCH 2/2] champs: put champ label in numeric fields validation messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces > La valeur du champ doit être un nombre entier (sans chiffres après > la virgule) by > La valeur du champ « Nombre de parents » doit être un nombre entier > (sans chiffres après la virgule) --- app/models/champs/decimal_number_champ.rb | 8 +++++++- app/models/champs/integer_number_champ.rb | 9 ++++++++- spec/models/champs/decimal_number_champ_spec.rb | 3 ++- spec/models/champs/integer_number_champ_spec.rb | 4 +++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index 199e38079..d7063cf8e 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -1,5 +1,11 @@ class Champs::DecimalNumberChamp < Champ - validates :value, numericality: { allow_nil: true, allow_blank: true } + validates :value, numericality: { + allow_nil: true, + allow_blank: true, + message: -> (object, data) { + "« #{object.libelle} » " + object.errors.generate_message(data[:attribute].downcase, :not_a_number) + } + } def for_export processed_value diff --git a/app/models/champs/integer_number_champ.rb b/app/models/champs/integer_number_champ.rb index 4717c24df..c2c0ea254 100644 --- a/app/models/champs/integer_number_champ.rb +++ b/app/models/champs/integer_number_champ.rb @@ -1,5 +1,12 @@ class Champs::IntegerNumberChamp < Champ - validates :value, numericality: { only_integer: true, allow_nil: true, allow_blank: true } + validates :value, numericality: { + only_integer: true, + allow_nil: true, + allow_blank: true, + message: -> (object, data) { + "« #{object.libelle} » " + object.errors.generate_message(data[:attribute].downcase, :not_an_integer) + } + } def for_export processed_value diff --git a/spec/models/champs/decimal_number_champ_spec.rb b/spec/models/champs/decimal_number_champ_spec.rb index 8ec0ebdaf..7cc511b82 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Champs::DecimalNumberChamp do - subject { Champs::DecimalNumberChamp.new(value: value) } + subject { build(:champ_decimal_number, value: value).tap(&:valid?) } describe '#valid?' do context 'when the value is integer number' do @@ -20,6 +20,7 @@ describe Champs::DecimalNumberChamp do let(:value) { 'toto' } it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre"]) } end context 'when the value is blank' do diff --git a/spec/models/champs/integer_number_champ_spec.rb b/spec/models/champs/integer_number_champ_spec.rb index 6a7152759..c61b25576 100644 --- a/spec/models/champs/integer_number_champ_spec.rb +++ b/spec/models/champs/integer_number_champ_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe Champs::IntegerNumberChamp do - subject { Champs::IntegerNumberChamp.new(value: value) } + subject { build(:champ_integer_number, value: value).tap(&:valid?) } describe '#valid?' do context 'when the value is integer number' do @@ -14,12 +14,14 @@ describe Champs::IntegerNumberChamp do let(:value) { 2.6 } it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) } end context 'when the value is not a number' do let(:value) { 'toto' } it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit être un nombre entier (sans chiffres après la virgule)"]) } end context 'when the value is blank' do