Module: CoreHtml5FormHelper
- Defined in:
- app/helpers/core_html5_form_helper.rb
Overview
Helpful methods HTML5 elements in a form
Instance Method Summary collapse
- #html5_checkbox(model, field, options = {}) ⇒ Object
- #html5_checkbox_label_tag(model, field, value, options = {}) ⇒ Object
- #html5_field_id(model, field, options = {}) ⇒ Object
- #html5_field_name(model, field, options = {}) ⇒ Object
- #html5_field_place_holder(model, field) ⇒ Object
- #html5_label_tag(model, field, value, options = {}) ⇒ Object
- #html5_password(model, field, options = {}) ⇒ Object abstract
- #html5_text_field(model, field, options = {}) ⇒ Object
- #html5_text_field_options(model, field, options) ⇒ Object
Instance Method Details
#html5_checkbox(model, field, options = {}) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/helpers/core_html5_form_helper.rb', line 124 def html5_checkbox(model, field, = {}) classes = [:classes] || %w[col-sm-12 col-lg-6] value = model.send(field) [:disabled] ||= false properties = { class: 'form-check-input', id: html5_field_id(model, field, ), name: html5_field_name(model, field, ), type: :checkbox, disabled: [:disabled] } properties[:checked] = true if model.send(field) checkbox_tag = tag(:input, properties) content_tag(:div, class: (%w[form-check mt-4] + classes).join(' ')) do concat(content_tag(:label) do concat(checkbox_tag) concat(html5_checkbox_label_tag(model, field, value, input_classes: classes)) end) end end |
#html5_checkbox_label_tag(model, field, value, options = {}) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'app/helpers/core_html5_form_helper.rb', line 145 def html5_checkbox_label_tag(model, field, value, = {}) # dont do a label if we are in default browser mode return if [:input_classes].present? && [:input_classes].include?('browser-default') # or if we have a prompt with now value place_holder = html5_field_place_holder(model, field) return if place_holder.blank? && value.blank? && [:prompt].present? error = model.errors[field] key = "ui_form.#{model.class.to_s.underscore}.labels.#{field}" classes = value.nil? && place_holder.blank? ? '' : 'active' classes += error.present? ? ' invalid red-text' : ' valid' [:class] = classes ['data-error'] = error.join(', ') if error.present? content_tag(:span, ) do concat(I18n.exists?(key) ? I18n.t(key) : field.to_s.humanize) end end |
#html5_field_id(model, field, options = {}) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'app/helpers/core_html5_form_helper.rb', line 98 def html5_field_id(model, field, = {}) return [:form_id] if [:form_id].present? # TODO: Need to handle the other side of the 1:M use case where # the field name needs to end in _ids, not _id. field = "#{field}_id" if model.class.reflect_on_association(field).present? if [:index].present? if [:array_name].present? if [:base_name].present? "#{[:form_id_prefix]}#{[:base_name]}_#{[:array_name]}_#{[:index]}_#{field}" else "#{[:form_id_prefix]}#{[:array_name]}_#{[:index]}_#{field}" end else "#{[:form_id_prefix]}#{model.class.to_s.underscore}_#{[:index]}_#{field}" end else "#{[:form_id_prefix]}#{model.class.to_s.underscore}_#{field}" end end |
#html5_field_name(model, field, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/helpers/core_html5_form_helper.rb', line 77 def html5_field_name(model, field, = {}) return [:form_name] if [:form_name].present? # TODO: Need to handle the other side of the 1:M use case where # the field name needs to end in _ids, not _id. field = "#{field}_id" if model.class.reflect_on_association(field).present? if [:index].present? if [:array_name].present? if [:base_name].present? "#{[:base_name]}[#{[:array_name]}[#{[:index]}][#{field}]]" else "#{[:array_name]}[#{[:index]}][#{field}]" end else "#{model.class.to_s.underscore}[#{[:index]}][#{field}]" end else "#{model.class.to_s.underscore}[#{field}]" end end |
#html5_field_place_holder(model, field) ⇒ Object
119 120 121 122 |
# File 'app/helpers/core_html5_form_helper.rb', line 119 def html5_field_place_holder(model, field) place_holder_key = "ui_form.#{model.class.to_s.underscore}.placeholders.#{field}" I18n.exists?(place_holder_key) ? I18n.t(place_holder_key) : nil end |
#html5_label_tag(model, field, value, options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/helpers/core_html5_form_helper.rb', line 34 def html5_label_tag(model, field, value, = {}) # dont do a label if we are in default browser mode return if [:no_label] return if [:input_classes].present? && [:input_classes].include?('browser-default') # or if we have a prompt with now value place_holder = field_placeholder_text(model, field, default: [:placeholder]) return if place_holder.blank? && value.blank? && [:prompt].present? error = model.errors[field] key = "ui_form.#{model.class.to_s.underscore}.labels.#{field}" classes = value.nil? && place_holder.blank? ? '' : 'active' classes += error.present? ? ' invalid red-text' : ' valid' [:class] = classes [:for] = html5_field_id(model, field, ) ['data-error'] = error.join(', ') if error.present? content_tag(:label, ) do concat(I18n.exists?(key) ? I18n.t(key) : field.to_s.humanize) concat(" #{error.join(', ')}") if error.present? end end |
#html5_password(model, field, options = {}) ⇒ Object
This method is abstract.
Render a password field
TODO:
Combine this with the html5_text_field, the only thing different is the field type
9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/helpers/core_html5_form_helper.rb', line 9 def html5_password(model, field, = {}) classes = [:classes] || %w[col-sm-12 col-lg-6] value = model.send(field) [:type] = :password [:place_holder] ||= mask_value(value) = (model, field, ) content_tag(:div, class: (%w[form-floating form-floating-outline mb-3 col] + classes).join(' ')) do concat(tag(:input, )) concat(form_label_tag(model, field, value, )) end end |
#html5_text_field(model, field, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/helpers/core_html5_form_helper.rb', line 21 def html5_text_field(model, field, = {}) classes = [:classes] || %w[col-sm-12 col-lg-6] value = model.send(field) [:type] ||= :text [:value] = value [:disabled] ||= false = (model, field, ) content_tag(:div, class: (%w[form-floating form-floating-outline mb-3 col] + classes).join(' ')) do concat(tag(:input, )) concat(html5_label_tag(model, field, value, )) end end |
#html5_text_field_options(model, field, options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'app/helpers/core_html5_form_helper.rb', line 56 def (model, field, ) hint_key = "ui_form.#{model.class.to_s.underscore}.hints.#{field}" if I18n.exists?(hint_key) classes = %w[form-control validate tooltipped] [:data] = { tooltip: I18n.t(hint_key), position: :top } else classes = %w[form-control validate] end classes += [:input_classes] if [:input_classes].present? [:name] = html5_field_name(model, field, ) [:id] = html5_field_id(model, field, ) place_holder = [:place_holder] || html5_field_place_holder(model, field) if place_holder.present? classes << 'active' [:placeholder] = place_holder end classes << 'active' if [:value].present? [:class] = classes.uniq end |