Skip to content

Commit

Permalink
Add validation for future start date
Browse files Browse the repository at this point in the history
  • Loading branch information
AbigailMcP authored and AbigailMcP committed Aug 23, 2024
1 parent 06cc8fc commit 5e126ef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class StartDateForm < Form
attribute :start_date, :date

validates :start_date, presence: {message: i18n_error_message(:presence)}
validate :start_year_has_four_digits, if: -> { start_date.present? }
validates :start_date, comparison: {less_than: ->(_) { Date.tomorrow }, message: i18n_error_message(:date_not_in_future)}, if: :start_date
validate :start_year_has_four_digits, if: :start_date

def initialize(journey_session:, journey:, params:)
super
Expand All @@ -34,7 +35,7 @@ def nursery_name

def start_year_has_four_digits
if start_date.year < 1000
errors.add(:start_date, "Year must include 4 numbers")
errors.add(:start_date, i18n_errors_path(:year_must_have_4_digits))
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,8 @@ en:
hint: For example, 27 3 2024.
errors:
presence: Provide a date in the format 27 3 2024
date_not_in_future: Start date cannot be in the future
year_must_have_4_digits: Year must include 4 numbers
child_facing:
question: Confirm %{first_name} spends most of their time in their job working directly with children
hint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
describe "validations" do
it { should validate_presence_of(:start_date).with_message("Provide a date in the format 27 3 2024") }

context "with a date in the future" do
it do
is_expected.not_to(
allow_value(Date.tomorrow)
.for(:start_date)
.with_message("Start date cannot be in the future")
)
end
end

context "when the year doesn't have 4 digits" do
let(:day) { 1 }
let(:month) { 1 }
Expand Down

0 comments on commit 5e126ef

Please sign in to comment.