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/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 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