Generate labels for attribute automatically
I'm not sure why it isn't already done by foundation's form builder. It doesn't make any sense to change an ID of a form field without changing the `for` attribute of its label.
This commit is contained in:
@@ -9,7 +9,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
|
||||
|
||||
%i[text_field text_area cktext_area number_field password_field email_field].each do |field|
|
||||
define_method field do |attribute, options = {}|
|
||||
label_with_hint(attribute, options) +
|
||||
label_with_hint(attribute, options.merge(label_options: label_options_for(options))) +
|
||||
super(attribute, options.merge(
|
||||
label: false, hint: false,
|
||||
aria: { describedby: help_text_id(attribute, options) }
|
||||
@@ -21,7 +21,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
|
||||
if options[:label] != false
|
||||
label = content_tag(:span, label_text(object, attribute, options[:label]), class: "checkbox")
|
||||
|
||||
super(attribute, options.merge(label: label))
|
||||
super(attribute, options.merge(label: label, label_options: label_options_for(options)))
|
||||
else
|
||||
super
|
||||
end
|
||||
@@ -42,6 +42,16 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
|
||||
end
|
||||
end
|
||||
|
||||
def label_options_for(options)
|
||||
label_options = options[:label_options] || {}
|
||||
|
||||
if options[:id]
|
||||
{ for: options[:id] }.merge(label_options)
|
||||
else
|
||||
label_options
|
||||
end
|
||||
end
|
||||
|
||||
def help_text(attribute, options)
|
||||
if options[:hint]
|
||||
content_tag :span, options[:hint], class: "help-text", id: help_text_id(attribute, options)
|
||||
|
||||
Reference in New Issue
Block a user