add validation on decimal max 3 digits after coma
This commit is contained in:
parent
a60bc5d630
commit
a3ab6dd257
5 changed files with 25 additions and 2 deletions
|
@ -1,6 +1,14 @@
|
||||||
class Champs::DecimalNumberChamp < Champ
|
class Champs::DecimalNumberChamp < Champ
|
||||||
before_validation :format_value
|
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_nil: true,
|
||||||
allow_blank: true,
|
allow_blank: true,
|
||||||
message: -> (object, _data) {
|
message: -> (object, _data) {
|
||||||
|
|
|
@ -731,6 +731,7 @@ en:
|
||||||
# one: "Aucune parcelle cadastrale sur la zone sélectionnée"
|
# one: "Aucune parcelle cadastrale sur la zone sélectionnée"
|
||||||
# other: "Aucune parcelle cadastrale sur les zones sélectionnées"
|
# 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_float: "must have maximum 3 digits after the decimal point"
|
||||||
not_a_date: "must be a correct date"
|
not_a_date: "must be a correct date"
|
||||||
not_a_datetime: "must be a correct datetime"
|
not_a_datetime: "must be a correct datetime"
|
||||||
blank: "can't be blank"
|
blank: "can't be blank"
|
||||||
|
|
|
@ -738,6 +738,7 @@ fr:
|
||||||
one: "Aucune parcelle cadastrale sur la zone sélectionnée"
|
one: "Aucune parcelle cadastrale sur la zone sélectionnée"
|
||||||
other: "Aucune parcelle cadastrale sur les zones sélectionnées"
|
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_float: "doit comprendre maximum 3 chiffres après la virgule"
|
||||||
not_a_date: "doit être une date correctement formatée"
|
not_a_date: "doit être une date correctement formatée"
|
||||||
not_a_datetime: "doit être une date et heure correctement formatée"
|
not_a_datetime: "doit être une date et heure correctement formatée"
|
||||||
blank: "doit être rempli"
|
blank: "doit être rempli"
|
||||||
|
|
|
@ -18,7 +18,14 @@ describe Champs::DecimalNumberChamp do
|
||||||
let(:value) { 'toto' }
|
let(:value) { 'toto' }
|
||||||
|
|
||||||
it { is_expected.to_not be_valid }
|
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
|
end
|
||||||
|
|
||||||
context 'when the value is blank' do
|
context 'when the value is blank' do
|
||||||
|
|
|
@ -54,6 +54,12 @@ describe Logic::ChampValue do
|
||||||
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:number) }
|
it { expect(champ_value(champ.stable_id).type([champ.type_de_champ])).to eq(:number) }
|
||||||
it { is_expected.to eq(42.01) }
|
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
|
context 'with invalid value' do
|
||||||
before { champ.value = 'racine de 2' }
|
before { champ.value = 'racine de 2' }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue