Skip to content

Commit 3cde79e

Browse files
committed
Improve invalid date validation
This improves the validation when adding an invalid session date to improve the experience for users. Jira-Issue: MAV-2215
1 parent d484186 commit 3cde79e

2 files changed

Lines changed: 29 additions & 17 deletions

File tree

app/controllers/draft_sessions_controller.rb

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,21 @@ def handle_dates
8484
@draft_session.session_dates.delete_at(index)
8585
jump_to("dates")
8686
else
87-
session_date.assign_attributes(remove_invalid_date(attributes))
87+
# We need to do this here to get around the multi-parameter
88+
# assignment error being raised if we don't validate before
89+
# assignment.
90+
year = attributes["value(1i)"]
91+
month = attributes["value(2i)"]
92+
day = attributes["value(3i)"]
93+
94+
next if day.blank? && month.blank? && year.blank?
95+
96+
begin
97+
value = Date.new(year.to_i, month.to_i, day.to_i)
98+
session_date.assign_attributes(value:)
99+
rescue StandardError
100+
session_date.errors.add(:value, :blank)
101+
end
88102
end
89103
end
90104
end
@@ -160,20 +174,4 @@ def send_invitations_at_validator
160174
params: update_params
161175
)
162176
end
163-
164-
def remove_invalid_date(hash)
165-
return hash if hash.blank?
166-
167-
keys = %w[value(1i) value(2i) value(3i)]
168-
169-
if keys.all? { hash.key?(it) }
170-
begin
171-
Date.new(*keys.map { hash[it].to_i })
172-
rescue StandardError
173-
keys.each { hash.delete(it) }
174-
end
175-
end
176-
177-
hash
178-
end
179177
end

spec/features/manage_school_sessions_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
when_i_click_on_schedule_sessions
2121
then_i_see_the_dates_page
2222

23+
when_i_add_an_invalid_date
24+
then_i_see_a_validation_error
25+
2326
when_i_choose_the_dates
2427
then_i_see_the_confirmation_page
2528

@@ -205,6 +208,17 @@ def then_i_see_the_dates_page
205208
expect(page).to have_content("When will sessions be held?")
206209
end
207210

211+
def when_i_add_an_invalid_date
212+
fill_in "Day", with: "99"
213+
fill_in "Month", with: "99"
214+
fill_in "Year", with: "99"
215+
click_on "Continue"
216+
end
217+
218+
def then_i_see_a_validation_error
219+
expect(page).to have_content("Enter a date")
220+
end
221+
208222
def when_i_choose_the_dates
209223
fill_in "Day", with: "10"
210224
fill_in "Month", with: "03"

0 commit comments

Comments
 (0)