Skip to content

Commit

Permalink
Merge pull request #3110 from DFE-Digital/LUPEYALPHA-880-ey-start-dat…
Browse files Browse the repository at this point in the history
…e-validations

Start date validations
  • Loading branch information
AbigailMcP authored Aug 23, 2024
2 parents 00794f1 + 140db20 commit 937dfce
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class StartDateForm < Form
attribute :start_date, :date

validates :start_date, presence: {message: i18n_error_message(:presence)}
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 @@ -30,6 +32,12 @@ def save
def nursery_name
EligibleEyProvider.find_by(urn: answers.nursery_urn)&.nursery_name
end

def start_year_has_four_digits
if start_date.year < 1000
errors.add(:start_date, i18n_errors_path(:year_must_have_4_digits))
end
end
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 @@ -34,9 +34,10 @@
click_button "Continue"

expect(page.current_path).to eq "/early-years-payment-provider/start-date"
fill_in("Day", with: "1")
fill_in("Month", with: "12")
fill_in("Year", with: "2024")
date = Date.yesterday
fill_in("Day", with: date.day)
fill_in("Month", with: date.month)
fill_in("Year", with: date.year)
click_button "Continue"

expect(page.current_path).to eq "/early-years-payment-provider/child-facing"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@

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 }
let(:year) { 24 }

it "returns an error" do
subject.save
expect(subject.errors[:start_date]).to include("Year must include 4 numbers")
end
end
end

describe "#save" do
Expand Down

0 comments on commit 937dfce

Please sign in to comment.