Skip to content

Commit

Permalink
Fix min,max constraints not validated on integer & decimal champ
Browse files Browse the repository at this point in the history
  • Loading branch information
maatinito committed Dec 22, 2023
1 parent bcee4e0 commit 955939e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
17 changes: 13 additions & 4 deletions app/models/champs/decimal_number_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions app/models/champs/integer_number_champ.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 955939e

Please sign in to comment.