Skip to content

Commit

Permalink
fix date min max validation
Browse files Browse the repository at this point in the history
  • Loading branch information
maatinito committed Jan 26, 2024
1 parent b524886 commit 6e1a852
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/models/champs/date_champ.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
class Champs::DateChamp < Champ
before_validation :convert_to_iso8601, unless: -> { validation_context == :prefill }
validate :iso_8601
validate :min_max_validation

def min_max_validation
return if value.blank?

if type_de_champ.min.present? && Date.parse(value) < Date.parse(type_de_champ.min)
errors.add(:value, :greater_than_or_equal_to, value: value, count: I18n.l(Date.parse(type_de_champ.min), format: :long))
end
if type_de_champ.max.present? && Date.parse(value) > Date.parse(type_de_champ.max)
errors.add(:value, :less_than_or_equal_to, value: value, count: I18n.l(Date.parse(type_de_champ.max), format: :long))
end
end


def search_terms
# Text search is pretty useless for dates so we’re not including these champs
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ en:
not_a_date: "must be a correct date"
not_a_datetime: "must be a correct datetime"
blank: "can't be blank"
date:
formats:
default: "%B %e, %Y"
long: "%B %e, %Y"
time:
formats:
default: "%B %d %Y %R"
Expand Down
4 changes: 4 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,10 @@ fr:
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"
date:
formats:
default: "%d/%m/%Y"
long: "%d %B %Y"
time:
formats:
default: "%d %B %Y %R"
Expand Down

0 comments on commit 6e1a852

Please sign in to comment.