module GovUkDateFields
class FormFields
VALID_OPTIONS = [:legend_text, :legend_class, :form_hint_text, :id, :placeholders, :error_messages]
DATE_SEGMENTS = {
day: '_dd',
month: '_mm',
year: '_yyyy',
}
DEFAULT_PLACEHOLDERS = {
day: 'DD',
month: 'MM',
year: 'YYYY'
}
def initialize(form, object_name, attribute, options={})
@form = form
@object = form.object
@object_name = object_name
@attribute = attribute
@options = options
@day_value = @object.send("#{@attribute}_dd")
@month_value = @object.send("#{@attribute}_mm")
@year_value = @object.send("#{@attribute}_yyyy")
@form_hint_text = @options[:form_hint_text] || "For example, 31 3 1980"
@fieldset_required = false
@fieldset_id = @options[:id]
@error_messages = @options[:error_messages]
parse_options
end
def raw_output
if fieldset_required?
generate_input_fields
else
generate_old_style_input_fields
end
end
def output
raw_output.html_safe
end
private
def error_for_attr?
@object.errors.keys.include?(@attribute)
end
def fieldset_required?
@fieldset_required
end
def generate_old_style_input_fields
%Q[
#{@form.text_field(@attribute, field_options(@day_value, html_id(:day), html_name(:day), placeholder(:day), 2))}
#{@form.text_field(@attribute, field_options(@month_value, html_id(:month), html_name(:month), placeholder(:month), 3))}
#{@form.text_field(@attribute, field_options(@year_value, html_id(:year), html_name(:year), placeholder(:year), 4))}
]
end
def generate_start_fieldset
%Q|
#{generate_fieldset_tag}
#{generate_legend_tag}#{@options[:legend_text]}
"
end
def generate_legend_tag
if @options.key?(:legend_class)
%Q|