Skip to content

Commit 8af4cd2

Browse files
committed
Add date validation for TimePeriodDateRange start/end fields
Include DateValidation concern to handle invalid multiparameter date assignment (e.g. month=23) gracefully with validation errors instead of raising ActiveRecord::MultiparameterAssignmentErrors.
1 parent addcb68 commit 8af4cd2

5 files changed

Lines changed: 61 additions & 1 deletion

File tree

app/models/block/time_period_date_range.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module Block
22
class TimePeriodDateRange < ApplicationRecord
33
self.table_name = "block_time_period_date_ranges"
44

5+
include DateValidation
6+
date_attributes :start, :end
7+
58
belongs_to :edition, class_name: "Block::TimePeriodEdition"
69

710
validates :start, presence: true

config/locales/en.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ en:
4242
alphanumeric: "must contain at least one letter or number"
4343
lead_organisation_id:
4444
blank: "cannot be blank"
45+
block/time_period_date_range:
46+
attributes:
47+
start:
48+
blank: "Enter a start date"
49+
invalid_date: "Start date is not a valid date"
50+
end:
51+
blank: "Enter an end date"
52+
invalid_date: "End date is not a valid date"
4553
attributes:
4654
edition/document:
4755
title:

features/block/time_period/create_block.feature

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,11 @@ Feature: Create "Time period" content block (Block architecture)
1717

1818
Scenario: Editor must supply valid edition details
1919
When I submit the new-style time period form incorrectly
20-
Then I see the errors messages describing the problems with the new-style edition
20+
Then I see the errors messages describing the problems with the new-style edition
21+
22+
Scenario: Editor must supply valid date range dates
23+
When I fill the new-style time period form correctly
24+
And I proceed to add a date range for the new-style Time period
25+
And I supply an invalid date for the date range
26+
And I save and continue
27+
Then I see the error message for the invalid date

features/step_definitions/block/time_period_steps.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,23 @@
5959
).render,
6060
)
6161
end
62+
63+
When("I supply an invalid date for the date range") do
64+
# Start date with invalid month (23)
65+
fill_in "edition[date_range_attributes][start(3i)]", with: "6"
66+
fill_in "edition[date_range_attributes][start(2i)]", with: "23"
67+
fill_in "edition[date_range_attributes][start(1i)]", with: "2025"
68+
select "09", from: "start_hour"
69+
select "00", from: "start_minute"
70+
71+
# Valid end date
72+
fill_in "edition[date_range_attributes][end(3i)]", with: "5"
73+
fill_in "edition[date_range_attributes][end(2i)]", with: "4"
74+
fill_in "edition[date_range_attributes][end(1i)]", with: "2026"
75+
select "17", from: "end_hour"
76+
select "30", from: "end_minute"
77+
end
78+
79+
Then("I see the error message for the invalid date") do
80+
expect(page).to have_content("Start date is not a valid date")
81+
end

spec/unit/app/models/block/time_period_date_range_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@
1111
it { is_expected.to validate_presence_of(:start) }
1212
it { is_expected.to validate_presence_of(:end) }
1313

14+
describe "invalid date validation" do
15+
it "is invalid when start date has invalid month" do
16+
date_range = described_class.new(edition: edition)
17+
# Simulate multiparameter assignment with invalid month (23)
18+
date_range.start = { 1 => 2025, 2 => 23, 3 => 6, 4 => 0, 5 => 0 }
19+
date_range.end = Time.zone.parse("2026-04-05 23:59")
20+
21+
expect(date_range).not_to be_valid
22+
expect(date_range.errors[:start]).to include("Start date is not a valid date")
23+
end
24+
25+
it "is invalid when end date has invalid day" do
26+
date_range = described_class.new(edition: edition)
27+
date_range.start = Time.zone.parse("2025-04-06 00:00")
28+
# Simulate multiparameter assignment with invalid day (32)
29+
date_range.end = { 1 => 2025, 2 => 4, 3 => 32, 4 => 23, 5 => 59 }
30+
31+
expect(date_range).not_to be_valid
32+
expect(date_range.errors[:end]).to include("End date is not a valid date")
33+
end
34+
end
35+
1436
describe "end_date_after_start_date" do
1537
it "is invalid when end is before start" do
1638
date_range = described_class.new(

0 commit comments

Comments
 (0)