Skip to content

Instantly share code, notes, and snippets.

@tizoc
Created September 30, 2010 10:37
Show Gist options
  • Save tizoc/604378 to your computer and use it in GitHub Desktop.
Save tizoc/604378 to your computer and use it in GitHub Desktop.
module Bureaucrat
module Quickfields
def date(name, options = {})
field name, DateField.new(options)
end
end
module Fields
class DateField < RegexField
DATE_RE = %r{^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$}
set_error :invalid, "Enter a valid date (YYYY-MM-DD)"
def initialize(options = {})
super(DATE_RE, options)
end
def widget_attrs(widget)
{ :class => 'datepicker' }
end
def clean(value)
value = super(value)
Date.parse(value)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment