From 955939ecb6661bf269eab653ed127aafad7ea663 Mon Sep 17 00:00:00 2001 From: Christian Lautier <15379878+maatinito@users.noreply.github.com> Date: Thu, 21 Dec 2023 16:28:45 -1000 Subject: [PATCH] Fix min,max constraints not validated on integer & decimal champ --- app/models/champs/decimal_number_champ.rb | 17 +++++++++++++---- app/models/champs/integer_number_champ.rb | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 9 deletions(-) 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