From a3ab6dd25750c5dd00416446069c04e818ee6b4c Mon Sep 17 00:00:00 2001 From: Lisa Durand Date: Mon, 18 Mar 2024 18:10:26 +0100 Subject: [PATCH] add validation on decimal max 3 digits after coma --- app/models/champs/decimal_number_champ.rb | 10 +++++++++- config/locales/en.yml | 1 + config/locales/fr.yml | 1 + spec/models/champs/decimal_number_champ_spec.rb | 9 ++++++++- spec/models/logic/champ_value_spec.rb | 6 ++++++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/app/models/champs/decimal_number_champ.rb b/app/models/champs/decimal_number_champ.rb index a47e28862..3339555d9 100644 --- a/app/models/champs/decimal_number_champ.rb +++ b/app/models/champs/decimal_number_champ.rb @@ -1,6 +1,14 @@ class Champs::DecimalNumberChamp < Champ before_validation :format_value - validates :value, numericality: { + validates :value, format: { + with: /\A-?[0-9]+([\.,][0-9]{1,3})?\z/, + allow_nil: true, + 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) + } + }, numericality: { allow_nil: true, allow_blank: true, message: -> (object, _data) { diff --git a/config/locales/en.yml b/config/locales/en.yml index 65529d663..f618a98fd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -731,6 +731,7 @@ en: # 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_a_float: "must have maximum 3 digits after the decimal point" 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 780264e06..0841eb838 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -738,6 +738,7 @@ fr: 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_a_float: "doit comprendre maximum 3 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 0ad8be67e..214353607 100644 --- a/spec/models/champs/decimal_number_champ_spec.rb +++ b/spec/models/champs/decimal_number_champ_spec.rb @@ -18,7 +18,14 @@ describe Champs::DecimalNumberChamp do 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(["« #{subject.libelle} » doit comprendre maximum 3 chiffres après la virgule", "« #{subject.libelle} » n'est pas un nombre"]) } + end + + context 'when the value has too many decimal' do + let(:value) { '2.6666' } + + it { is_expected.to_not be_valid } + it { expect(subject.errors[:value]).to eq(["« #{subject.libelle} » doit comprendre maximum 3 chiffres après la virgule"]) } end context 'when the value is blank' do diff --git a/spec/models/logic/champ_value_spec.rb b/spec/models/logic/champ_value_spec.rb index 254ac5bed..f7b12020f 100644 --- a/spec/models/logic/champ_value_spec.rb +++ b/spec/models/logic/champ_value_spec.rb @@ -54,6 +54,12 @@ describe Logic::ChampValue do it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:number) } it { is_expected.to eq(42.01) } + context 'with invalid value with too many digits after the decimal point' do + before { champ.value = '42.1234' } + + it { is_expected.to be nil } + end + context 'with invalid value' do before { champ.value = 'racine de 2' }