Permalink
Browse files

Validator and JS date picker changes for first run creation date

  • Loading branch information...
claire-gtr authored and Aquaj committed Dec 4, 2018
1 parent cc85f06 commit 098b69ac985e209de95d4f7794a8fdefa6301310
Showing with 31 additions and 2 deletions.
  1. +18 −2 app/assets/javascripts/form/dates.js.coffee
  2. +13 −0 app/validators/date_format_validator.rb
@@ -1,15 +1,31 @@
(($) ->

unless Modernizr.touch and Modernizr.inputtypes.date
unless Modernizr.touch

# Initializes date fields
$(document).ready ->
$(".text-datepicker").each ->
$(this).attr('type', 'text')

$(document).on "focusin click keyup change", "input[type='text'].text-datepicker", (event) ->
element = $(this)
if element.attr("autocomplete") isnt "off"
locale = element.attr("lang") or I18n.locale.slice(0, 2) # until we get corresponding locale codes
options = {}
$.extend options, $.datepicker.regional[locale],
dateFormat: "dd-mm-yy"
maxDate: element.data('max-date')
element.datepicker options
element.attr "autocomplete", "off"
return

$(document).on "focusin click keyup change", "input[type='date']", (event) ->
element = $(this)
if element.attr("autocomplete") isnt "off"
locale = element.attr("lang") or I18n.locale.slice(0, 2) # until we get corresponding locale codes
options = {}
$.extend options, $.datepicker.regional[locale],
dateFormat: "yy-mm-dd"
dateFormat: "dd-mm-yy"
maxDate: element.data('max-date')
element.datepicker options
element.attr "autocomplete", "off"
@@ -0,0 +1,13 @@
class DateFormatValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value.split(/\D/).size == 3
record.errors.add(attribute, :invalid)
end

begin
Date.parse(value)
rescue ArgumentError
record.errors.add(attribute, :invalid)
end
end
end

0 comments on commit 098b69a

Please sign in to comment.