forked from rubyforgood/human-essentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_range_controller.js
More file actions
53 lines (43 loc) · 2.15 KB
/
date_range_controller.js
File metadata and controls
53 lines (43 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// This Stimulus controller is used to handle custom validation for the date range input field.
// Litepicker.js manages the date range field and prevents invalid data when users interact with its calendar control.
// However, if a user tabs into the field and enters invalid data without triggering Litepicker events,
// Litepicker won't validate the input, leaving invalid data in the field.
// This controller ensures that in such cases, custom validation is performed to alert the user about invalid input.
//
// Note: The `data-skip-validation` attribute is used only in automated system tests to disable client-side validation.
// In real user interactions, if a user enters an invalid date and immediately hits Enter, the form submits before
// JS blur-based validation runs, so server-side validation is exercised as expected.
// However, in system tests (especially on CI), the JS blur validation always runs before form submission,
// making it impossible to test server-side validation for this scenario unless client-side validation is disabled.
// This attribute should only be set in test code.
import { Controller } from "@hotwired/stimulus";
import { DateTime } from "luxon";
export default class extends Controller {
static targets = ["input"];
connect() {
this.initialStart = this.inputTarget.dataset.initialStartDate;
this.initialEnd = this.inputTarget.dataset.initialEndDate;
this.format = "MMMM d, yyyy";
}
validate(event) {
event.preventDefault();
if (this.inputTarget.dataset.skipValidation === "true" || window.isLitepickerActive) {
return;
}
const value = this.inputTarget.value.trim();
const [startStr, endStr] = value.split(" - ").map((s) => s.trim());
const isValid = this.isValidDateRange(startStr, endStr);
if (!isValid) {
alert("Please enter a valid date range (e.g., January 1, 2024 - March 15, 2024).")
}
}
isValidDateRange(startStr, endStr) {
try {
const start = DateTime.fromFormat(startStr, this.format);
const end = DateTime.fromFormat(endStr, this.format);
return start.isValid && end.isValid && start <= end;
} catch (error) {
return false;
}
}
}