diff options
| author | Robert | 2017-06-23 11:43:56 +0200 |
|---|---|---|
| committer | Robert | 2017-06-23 11:51:48 +0200 |
| commit | 970954938043d8d73c4457ee2d91e22c0e422e65 (patch) | |
| tree | 8c633f0437b0bd76c21d40d3bc7225230d8ce61f /app | |
| parent | d51985fc2a7c2138fd12cb9116ebf05d8b0e7dac (diff) | |
| download | chouette-core-970954938043d8d73c4457ee2d91e22c0e422e65.tar.bz2 | |
JS for date validation
Diffstat (limited to 'app')
| -rw-r--r-- | app/assets/javascripts/calendars.coffee | 35 | ||||
| -rw-r--r-- | app/controllers/calendars_controller.rb | 2 | ||||
| -rw-r--r-- | app/models/calendar/calendar_date.rb | 2 |
3 files changed, 37 insertions, 2 deletions
diff --git a/app/assets/javascripts/calendars.coffee b/app/assets/javascripts/calendars.coffee new file mode 100644 index 000000000..ac5330cc8 --- /dev/null +++ b/app/assets/javascripts/calendars.coffee @@ -0,0 +1,35 @@ + +isEmpty = (x) -> x == '' + +daySelector = '#q_contains_date_3i' +monthSelector = '#q_contains_date_2i' +yearSelector = '#q_contains_date_1i' + +dateSelectors = [ daySelector, monthSelector, yearSelector ] + +checkDate = (args...) -> + vals = args.map((ele) -> ele.val()) + return if vals.find( isEmpty ) == "" # no checking needed, no checking possible yet + + dates = vals.map (x) -> parseInt(x) + return if isLegalDate(dates) + $('.alert').text('illegal date') + $('.alert').show() + +legalDaysPerMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] + +isLeapYear = (year) -> + (year % 4 == 0) && ((year % 100 != 0) || (year % 1000 == 0)) + +isLegalDate = (dates) -> + [day, month, year] = dates + return true if legalDaysPerMonth[month-1] >= day + return true if day == 29 && isLeapYear(year) + false + +defineChangeHandler = (selector) -> + $(selector).on 'change', -> + checkDate $(daySelector), $(monthSelector), $(yearSelector) + +$ -> + defineChangeHandler selector for selector in dateSelectors diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index a2bfe9b63..415ebcaeb 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -50,7 +50,7 @@ class CalendarsController < BreadcrumbController date << params[:q][key].to_i params[:q].delete(key) end - params[:q]['contains_date'] = Date.new(*date) + params[:q]['contains_date'] = Date.new(*date) rescue nil end end diff --git a/app/models/calendar/calendar_date.rb b/app/models/calendar/calendar_date.rb index 7537f562d..cfee95a25 100644 --- a/app/models/calendar/calendar_date.rb +++ b/app/models/calendar/calendar_date.rb @@ -3,7 +3,7 @@ class Calendar module IllegalDate attr_reader :year, :month, :day - def to_s + def to_s(*_args) "%d-%02d-%02d" % [year, month, day] end end |
