class Formulary::HtmlForm::Fields::DateInput

Public Class Methods

compatible_type() click to toggle source
# File lib/formulary/html_form/fields/date_input.rb, line 3
def self.compatible_type
  "date"
end
new(html_form, element) click to toggle source
# File lib/formulary/html_form/fields/date_input.rb, line 15
def initialize(html_form, element)
  @html_form, @element = html_form, element
  @min = @element.attributes['min'].try('value')
  @max = @element.attributes['max'].try('value')
end

Public Instance Methods

datetime_format() click to toggle source
# File lib/formulary/html_form/fields/date_input.rb, line 11
def datetime_format
  "%Y-%m-%d"
end
display_format() click to toggle source
# File lib/formulary/html_form/fields/date_input.rb, line 7
def display_format
  "YYYY-MM-DD"
end
error() click to toggle source
Calls superclass method
# File lib/formulary/html_form/fields/date_input.rb, line 25
def error
  return super if super.present?
  return "'#{label}' is not a properly formatted #{self.class.compatible_type}, please use #{display_format}" unless date_correct?
  return "'#{label}' must be a #{self.class.compatible_type} after #{@min}" unless min_correct?
  return "'#{label}' must be a #{self.class.compatible_type} before #{@max}" unless max_correct?
end
valid?() click to toggle source
Calls superclass method
# File lib/formulary/html_form/fields/date_input.rb, line 21
def valid?
  super && date_correct? && min_correct? && max_correct?
end

Protected Instance Methods

date_correct?() click to toggle source
# File lib/formulary/html_form/fields/date_input.rb, line 34
def date_correct?
  return true if @value.blank?
  Date.strptime(@value, datetime_format)
rescue ArgumentError => e
  if e.message.include?("invalid date")
    return false
  else
    raise
  end
end
max_correct?() click to toggle source
# File lib/formulary/html_form/fields/date_input.rb, line 52
def max_correct?
  return true if @value.blank?
  return true if @max.blank?
  return true if @value <= @max
  false
end
min_correct?() click to toggle source
# File lib/formulary/html_form/fields/date_input.rb, line 45
def min_correct?
  return true if @value.blank?
  return true if @min.blank?
  return true if @value >= @min
  false
end