Class: Symphonia::FormBuilder
- Inherits:
-
BootstrapForm::FormBuilder
- Object
- BootstrapForm::FormBuilder
- Symphonia::FormBuilder
- Defined in:
- lib/symphonia/form_builder.rb
Instance Method Summary collapse
- #calendar_field(name, options = {}) ⇒ Object
-
#error_class ⇒ Object
def generate_label(id, name, options, custom_label_col, group_layout) return if options.blank? # id is the caller’s options at the only place this method is called.
- #error_messages ⇒ Object
-
#label_text(name, options) ⇒ Object
def form_group_builder(method, options, html_options = nil) options.symbolize_keys! html_options.symbolize_keys! if html_options.
Instance Method Details
#calendar_field(name, options = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/symphonia/form_builder.rb', line 16 def calendar_field(name, = {}) [:data] ||= {} #{'date-clear-btn': true, 'date-format': 'yyyy-mm-dd'} [:data][:'date-clear-btn'] ||= true # options[:data][:'date-format'] ||= 'yyyy-mm-dd' text_field(name, .merge({ class: 'form-control datepicker', append: @template.fa_icon('calendar') })) end |
#error_class ⇒ Object
def generate_label(id, name, options, custom_label_col, group_layout)
return if .blank?
# id is the caller's options[:id] at the only place this method is called.
# The options argument is a small subset of the options that might have
# been passed to generate_label's caller, and definitely doesn't include
# :id.
[:for] = id if acts_like_form_tag
classes = [[:class]]
if layout_horizontal?(group_layout)
classes << "col-form-label"
classes << (custom_label_col || label_col)
elsif layout_inline?(group_layout)
classes << "mr-sm-2"
end
xname = name.to_s.remove(/_id$/)
case .delete(:required)
when true
classes << "required"
when nil, :default
classes << "required" if required_attribute?(object, name)
end
[:class] = classes.compact.join(" ").strip
.delete(:class) if [:class].empty?
if label_errors && has_error?(name)
= (name)
label_text = ([:text] || object.class.human_attribute_name(xname)).to_s.concat(" #{}")
[:class] = [[:class], "text-danger"].compact.join(" ")
label(name, label_text, .except(:text))
else
label(name, [:text], .except(:text))
end
end
132 133 134 |
# File 'lib/symphonia/form_builder.rb', line 132 def error_class 'is-invalid' end |
#error_messages ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/symphonia/form_builder.rb', line 4 def if object.respond_to?(:errors) && object.errors.any? list = content_tag(:p, I18n.t('activerecord.errors.template.body')).html_safe list += content_tag(:ul) do object.errors..collect do |error| content_tag(:li, error) end.join.html_safe end content_tag(:div, list, class: 'error_explanation') end end |
#label_text(name, options) ⇒ Object
def form_group_builder(method, options, html_options = nil)
.symbolize_keys!
.symbolize_keys! if
# Add control_class; allow it to be overridden by :control_class option
= ||
control_classes = .delete(:control_class) { control_class }
[:class] = [control_classes, [:class]].compact.join(" ")
[:class] << " is-invalid" if has_error?(method)
= (method, ) if acts_like_form_tag
wrapper_class = .delete(:wrapper_class)
= .delete(:wrapper)
help = .delete(:help)
icon = .delete(:icon)
label_col = .delete(:label_col)
control_col = .delete(:control_col)
layout = get_group_layout(.delete(:layout))
= {
id: [:id],
help: help,
icon: icon,
label_col: label_col,
control_col: control_col,
layout: layout,
class: wrapper_class
}
if .is_a?(Hash)
.merge!()
end
unless .delete(:skip_label)
if [:label].is_a?(Hash)
label_text = [:label].delete(:text)
label_class = [:label].delete(:class)
.delete(:label)
end
label_class ||= .delete(:label_class)
label_class = hide_class if .delete(:hide_label)
if [:label].is_a?(String)
label_text ||= .delete(:label)
end
.merge!(label: {
text: label_text,
class: label_class,
skip_required: .delete(:skip_required),
required: .delete(:required)
})
end
form_group(method, ) do
yield
end
end
85 86 87 88 89 90 91 92 93 |
# File 'lib/symphonia/form_builder.rb', line 85 def label_text(name, ) name = name.to_s.remove(/_id$/) if label_errors && error?(name) ([:text] || object.class.human_attribute_name(name)).to_s.concat(" #{(name)}") else [:text] end end |