diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index 2669dbc9b39..7a05abb33ff 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -23,11 +23,20 @@ class Champs::DecimalNumberChamp < Champ validates :value, numericality: { allow_nil: true, - allow_blank: true, - message: -> (object, _data) { - "« #{object.libelle} » " + object.errors.generate_message(:value, :not_a_number) - } + allow_blank: true } + validate :min_max_validation + + def min_max_validation + return if value.blank? + + if type_de_champ.min.present? && value.to_i < type_de_champ.min.to_i + errors.add(:value, :greater_than_or_equal_to, value: value, count: type_de_champ.min.to_i) + end + if type_de_champ.max.present? && value.to_i > type_de_champ.max.to_i + errors.add(:value, :less_than_or_equal_to, value: value, count: type_de_champ.max.to_i) + end + end def for_export processed_value diff --git a/app/models/champs/integer_number_champ.rb b/app/models/champs/integer_number_champ.rb index 7e123a646a0..94faa33120e 100644 --- a/app/models/champs/integer_number_champ.rb +++ b/app/models/champs/integer_number_champ.rb @@ -24,12 +24,20 @@ class Champs::IntegerNumberChamp < Champ validates :value, numericality: { only_integer: true, allow_nil: true, - 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) - } + allow_blank: true } + validate :min_max_validation + + def min_max_validation + return if value.blank? + + if type_de_champ.min.present? && value.to_i < type_de_champ.min.to_i + errors.add(:value, :greater_than_or_equal_to, value: value, count: type_de_champ.min.to_i) + end + if type_de_champ.max.present? && value.to_i > type_de_champ.max.to_i + errors.add(:value, :less_than_or_equal_to, value: value, count: type_de_champ.max.to_i) + end + end def for_export processed_value diff --git a/config/locales/en.yml b/config/locales/en.yml index 28a83c0d2c1..ca43a0226b9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -711,7 +711,7 @@ en: # cadastres_empty: # one: "Aucune parcelle cadastrale sur la zone sélectionnée" # other: "Aucune parcelle cadastrale sur les zones sélectionnées" - not_an_integer: "must be an integer (without decimal)" + # not_an_integer: "must be an integer (without decimal)" not_a_date: "must be a correct date" not_a_datetime: "must be a correct datetime" blank: "can't be blank" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 69ea93ac68f..39d3e36806c 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -722,7 +722,7 @@ fr: cadastres_empty: one: "Aucune parcelle cadastrale sur la zone sélectionnée" other: "Aucune parcelle cadastrale sur les zones sélectionnées" - not_an_integer: "doit être un nombre entier (sans chiffres après la virgule)" + # not_an_integer: "doit être un nombre entier (sans chiffres après la virgule)" not_a_date: "doit être une date correctement formatée" not_a_datetime: "doit être une date et heure correctement formatée" blank: "doit être rempli" diff --git a/spec/models/champs/decimal_number_champ_spec.rb b/spec/models/champs/decimal_number_champ_spec.rb index 0ad8be67e9d..c2ffdb07acb 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -1,5 +1,10 @@ describe Champs::DecimalNumberChamp do - subject { build(:champ_decimal_number, value: value).tap(&:valid?) } + let(:min) { nil } + let(:max) { nil } + + let(:type_de_champ) { create(:type_de_champ, min:, max:) } + + subject { build(:champ_decimal_number, value: value, type_de_champ:).tap(&:valid?) } describe '#valid?' do context 'when the value is integer number' do @@ -18,7 +23,7 @@ let(:value) { 'toto' } it { is_expected.to_not be_valid } - it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » n'est pas un nombre"]) } + it { expect(subject.errors[:value]).to eq(["n'est pas un nombre"]) } end context 'when the value is blank' do @@ -32,5 +37,37 @@ it { is_expected.to be_valid } end + + context "when max is specified" do + let(:max) { 10 } + context 'when the value is equal to max' do + let(:value) { '10' } + + it { is_expected.to be_valid } + end + + context 'when the value is greater than max' do + let(:value) { 11 } + + it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["doit être inférieur ou égal à 10"]) } + end + end + + context "when min is specified" do + let(:min) { 10 } + context 'when the value is equal to min' do + let(:value) { '10' } + + it { is_expected.to be_valid } + end + + context 'when the value is less than min' do + let(:value) { 9 } + + it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["doit être supérieur ou égal à 10"]) } + end + end end end diff --git a/spec/models/champs/integer_number_champ_spec.rb b/spec/models/champs/integer_number_champ_spec.rb index d7cb82569cc..d0eb2c05316 100644 --- a/spec/models/champs/integer_number_champ_spec.rb +++ b/spec/models/champs/integer_number_champ_spec.rb @@ -1,5 +1,10 @@ describe Champs::IntegerNumberChamp do - subject { build(:champ_integer_number, value: value).tap(&:valid?) } + let(:min) { nil } + let(:max) { nil } + + let(:type_de_champ) { create(:type_de_champ, min:, max:) } + + subject { build(:champ_integer_number, value: value, type_de_champ:).tap(&:valid?) } describe '#valid?' do context 'when the value is integer number' do @@ -12,14 +17,14 @@ 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)"]) } + it { expect(subject.errors[:value]).to eq(["doit être un nombre entier"]) } 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)"]) } + it { expect(subject.errors[:value]).to eq(["n'est pas un nombre"]) } end context 'when the value is blank' do @@ -33,5 +38,37 @@ it { is_expected.to be_valid } end + + context "when max is specified" do + let(:max) { 10 } + context 'when the value is equal to max' do + let(:value) { '10' } + + it { is_expected.to be_valid } + end + + context 'when the value is greater than max' do + let(:value) { 11 } + + it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["doit être inférieur ou égal à 10"]) } + end + end + + context "when min is specified" do + let(:min) { 10 } + context 'when the value is equal to min' do + let(:value) { '10' } + + it { is_expected.to be_valid } + end + + context 'when the value is less than min' do + let(:value) { 9 } + + it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["doit être supérieur ou égal à 10"]) } + end + end end end