Class: Hanami::Helpers::FormHelper::FormBuilder
- Inherits:
-
Object
- Object
- Hanami::Helpers::FormHelper::FormBuilder
- Includes:
- View::Helpers::EscapeHelper, View::Helpers::TagHelper
- Defined in:
- lib/hanami/helpers/form_helper/form_builder.rb
Overview
A range of convenient methods for building the fields within an HTML form, integrating with request params and template locals to populate the fields with appropriate values.
Instance Method Summary collapse
-
#button ⇒ String
Returns a button tag.
- #call(content, **attributes) ⇒ Object private
-
#check_box(name, **attributes) ⇒ String
Returns the tags for a check box.
-
#color_field(name, **attributes) ⇒ String
Returns a color input tag.
-
#datalist(name, values, list, **attributes) ⇒ String
Returns a datalist input tag.
-
#date_field(name, **attributes) ⇒ String
Returns a date input tag.
-
#datetime_field(name, **attributes) ⇒ String
Returns a datetime input tag.
-
#datetime_local_field(name, **attributes) ⇒ String
Returns a datetime-local input tag.
-
#email_field(name, **attributes) ⇒ String
Returns an email input tag.
-
#fields_for(name, *yield_args) {|the| ... } ⇒ Object
Applies the base input name to all fields within the given block.
-
#fields_for_collection(name) {|the, the, the| ... } ⇒ Object
Yields to the given block for each element in the matching collection value, and applies the base input name to all fields within the block.
-
#fieldset(**attributes, &block) ⇒ String
Returns a fieldset tag.
-
#file_field(name, **attributes) ⇒ String
Returns a file input tag.
-
#hidden_field(name, **attributes) ⇒ String
Returns a hidden input tag.
-
#image_button(source, **attributes) ⇒ String
Returns an image input tag, to be used as a visual button for the form.
-
#initialize(inflector:, form_attributes:, base_name: nil, values: Values.new) ⇒ self
constructor
private
Returns a new form builder.
-
#input ⇒ String
Returns an input tag.
-
#label(content = nil, **attributes, &block) ⇒ String
Returns a label tag.
-
#month_field(name, **attributes) ⇒ String
Returns a month input tag.
-
#number_field(name, **attributes) ⇒ String
Returns a number input tag.
-
#password_field(name, **attributes) ⇒ String
Returns a password input tag.
-
#radio_button(name, value, **attributes) ⇒ String
Returns a radio input tag.
-
#range_field(name, **attributes) ⇒ String
Returns a range input tag.
-
#search_field(name, **attributes) ⇒ String
Returns a search input tag.
-
#select(name, values, **attributes) ⇒ String
Returns a select input tag containing option tags for the given values.
-
#submit(content = nil, **attributes, &blk) ⇒ String
Returns a submit button tag.
-
#tel_field(name, **attributes) ⇒ String
Returns a telephone input tag.
-
#text_area(name, content = nil, **attributes) ⇒ String
Returns a textarea tag.
-
#text_field(name, **attributes) ⇒ String
(also: #input_text)
Returns a text input tag.
-
#time_field(name, **attributes) ⇒ String
Returns a time input tag.
-
#url_field(name, **attributes) ⇒ String
Returns a URL input tag.
-
#week_field(name, **attributes) ⇒ String
Returns a week input tag.
Constructor Details
#initialize(inflector:, form_attributes:, base_name: nil, values: Values.new) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new form builder.
109 110 111 112 113 114 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 109 def initialize(inflector:, form_attributes:, base_name: nil, values: Values.new) @base_name = base_name @values = values @form_attributes = form_attributes @inflector = inflector end |
Instance Method Details
#button(content, **attributes) ⇒ String #button(**attributes, &block) ⇒ String
Returns a button tag.
1123 1124 1125 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1123 def (...) tag.(...) end |
#call(content, **attributes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 118 def call(content, **attributes) attributes["accept-charset"] ||= DEFAULT_CHARSET method_override, original_form_method = _form_method(attributes) csrf_token, token = _csrf_token(values, attributes) tag.form(**attributes) do (+"").tap { |inner| inner << input(type: "hidden", name: "_method", value: original_form_method) if method_override inner << input(type: "hidden", name: "_csrf_token", value: token) if csrf_token inner << content }.html_safe end end |
#check_box(name, **attributes) ⇒ String
Returns the tags for a check box.
When editing a resource, the form automatically assigns the ‘checked` HTML attribute for the check box tag.
Returns a hidden input tag in preceding the check box input tag. This ensures that unchecked values are submitted with the form.
405 406 407 408 409 410 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 405 def check_box(name, **attributes) (+"").tap { |output| output << _hidden_field_for_check_box(name, attributes).to_s output << input(**_attributes_for_check_box(name, attributes)) }.html_safe end |
#color_field(name, **attributes) ⇒ String
Returns a color input tag.
429 430 431 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 429 def color_field(name, **attributes) input(**_attributes(:color, name, attributes)) end |
#datalist(name, values, list, **attributes) ⇒ String
Returns a datalist input tag.
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1068 def datalist(name, values, list, **attributes) = attributes.delete(:options) || {} datalist = attributes.delete(:datalist) || {} attributes[:list] = list datalist[:id] = list (+"").tap { |output| output << text_field(name, **attributes) output << tag.datalist(**datalist) { (+"").tap { |inner| values.each do |value, content| inner << tag.option(content, value: value, **) end }.html_safe } }.html_safe end |
#date_field(name, **attributes) ⇒ String
Returns a date input tag.
450 451 452 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 450 def date_field(name, **attributes) input(**_attributes(:date, name, attributes)) end |
#datetime_field(name, **attributes) ⇒ String
Returns a datetime input tag.
471 472 473 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 471 def datetime_field(name, **attributes) input(**_attributes(:datetime, name, attributes)) end |
#datetime_local_field(name, **attributes) ⇒ String
Returns a datetime-local input tag.
492 493 494 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 492 def datetime_local_field(name, **attributes) input(**_attributes(:"datetime-local", name, attributes)) end |
#email_field(name, **attributes) ⇒ String
Returns an email input tag.
576 577 578 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 576 def email_field(name, **attributes) input(**_attributes(:email, name, attributes)) end |
#fields_for(name, *yield_args) {|the| ... } ⇒ Object
Applies the base input name to all fields within the given block.
This can be helpful when generating a set of nested fields.
This is a convenience only. You can achieve the same result by including the base name at the beginning of each input name.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 172 def fields_for(name, *yield_args) new_base_name = [base_name, name.to_s].compact.join(INPUT_NAME_SEPARATOR) builder = self.class.new( base_name: new_base_name, values: values, form_attributes: form_attributes, inflector: inflector ) yield(builder, *yield_args) end |
#fields_for_collection(name) {|the, the, the| ... } ⇒ Object
Yields to the given block for each element in the matching collection value, and applies the base input name to all fields within the block.
Use this whenever generating form fields for an collection of nested fields.
228 229 230 231 232 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 228 def fields_for_collection(name, &block) _value(name).each_with_index do |value, index| fields_for("#{name}.#{index}", index, value, &block) end end |
#fieldset(**attributes, &block) ⇒ String
326 327 328 329 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 326 def fieldset(...) # This is here only for documentation purposes tag.fieldset(...) end |
#file_field(name, **attributes) ⇒ String
Returns a file input tag.
671 672 673 674 675 676 677 678 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 671 def file_field(name, **attributes) form_attributes[:enctype] = "multipart/form-data" attributes[:accept] = Array(attributes[:accept]).join(ACCEPT_SEPARATOR) if attributes.key?(:accept) attributes = {type: :file, name: _input_name(name), id: _input_id(name), **attributes} input(**attributes) end |
#hidden_field(name, **attributes) ⇒ String
Returns a hidden input tag.
637 638 639 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 637 def hidden_field(name, **attributes) input(**_attributes(:hidden, name, attributes)) end |
#image_button(source, **attributes) ⇒ String
Returns an image input tag, to be used as a visual button for the form.
For security reasons, you should use the absolute URL of the given image.
1146 1147 1148 1149 1150 1151 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1146 def (source, **attributes) attributes[:type] = :image attributes[:src] = sanitize_url(source) input(**attributes) end |
#input ⇒ String
Returns an input tag.
Generates an input tag without any special handling. For more convenience and other advanced features, see the other methods of the form builder.
1219 1220 1221 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1219 def input(...) tag.input(...) end |
#label(field_name, **attributes) ⇒ String #label(content, **attributes) ⇒ String #label(field_name, **attributes, &block) ⇒ String
Returns a label tag.
290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 290 def label(content = nil, **attributes, &block) for_attribute_given = attributes.key?(:for) attributes[:for] = _input_id(attributes[:for] || content) if content && !for_attribute_given content = inflector.humanize(content.to_s.split(INPUT_NAME_SEPARATOR).last) end tag.label(content, **attributes, &block) end |
#month_field(name, **attributes) ⇒ String
Returns a month input tag.
534 535 536 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 534 def month_field(name, **attributes) input(**_attributes(:month, name, attributes)) end |
#number_field(name, **attributes) ⇒ String
Returns a number input tag.
For this tag, you can make use of the ‘max`, `min`, and `step` HTML attributes.
699 700 701 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 699 def number_field(name, **attributes) input(**_attributes(:number, name, attributes)) end |
#password_field(name, **attributes) ⇒ String
Returns a password input tag.
861 862 863 864 865 866 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 861 def password_field(name, **attributes) attrs = {type: :password, name: _input_name(name), id: _input_id(name), value: nil, **attributes} attrs[:value] = EMPTY_STRING if attrs[:value].nil? input(**attrs) end |
#radio_button(name, value, **attributes) ⇒ String
Returns a radio input tag.
When editing a resource, the form automatically assigns the ‘checked` HTML attribute for the tag.
841 842 843 844 845 846 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 841 def (name, value, **attributes) attributes = {type: :radio, name: _input_name(name), value: value, **attributes} attributes[:checked] = true if _value(name).to_s == value.to_s input(**attributes) end |
#range_field(name, **attributes) ⇒ String
Returns a range input tag.
For this tag, you can make use of the ‘max`, `min`, and `step` HTML attributes.
722 723 724 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 722 def range_field(name, **attributes) input(**_attributes(:range, name, attributes)) end |
#search_field(name, **attributes) ⇒ String
Returns a search input tag.
798 799 800 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 798 def search_field(name, **attributes) input(**_attributes(:search, name, attributes)) end |
#select(name, values, **attributes) ⇒ String
Returns a select input tag containing option tags for the given values.
The values should be an enumerable of pairs of content (the displayed text for the option) and value (the value for the option) strings.
When editing a resource, automatically assigns the ‘selected` HTML attribute for any option tags matching the resource’s values.
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 989 def select(name, values, **attributes) # rubocop:disable Metrics/AbcSize = attributes.delete(:options) { {} } multiple = attributes[:multiple] attributes = {name: _select_input_name(name, multiple), id: _input_id(name), **attributes} prompt = .delete(:prompt) selected = .delete(:selected) input_value = _value(name) = [] << tag.option(prompt) if prompt already_selected = nil values.each do |content, value| if (multiple || !already_selected) && (already_selected = _select_option_selected?(value, selected, input_value, multiple)) << tag.option(content, value: value, selected: true, **) else << tag.option(content, value: value, **) end end tag.select(.join.html_safe, **attributes) end |
#submit(content, **attributes) ⇒ String #submit(**attributes, &blk) ⇒ String
Returns a submit button tag.
1190 1191 1192 1193 1194 1195 1196 1197 1198 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1190 def submit(content = nil, **attributes, &blk) if content.is_a?(::Hash) attributes = content content = nil end attributes = {type: :submit, **attributes} tag.(content, **attributes, &blk) end |
#tel_field(name, **attributes) ⇒ String
Returns a telephone input tag.
620 621 622 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 620 def tel_field(name, **attributes) input(**_attributes(:tel, name, attributes)) end |
#text_area(name, content = nil, **attributes) ⇒ String
Returns a textarea tag.
749 750 751 752 753 754 755 756 757 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 749 def text_area(name, content = nil, **attributes) if content.respond_to?(:to_hash) attributes = content content = nil end attributes = {name: _input_name(name), id: _input_id(name), **attributes} tag.textarea(content || _value(name), **attributes) end |
#text_field(name, **attributes) ⇒ String Also known as: input_text
Returns a text input tag.
776 777 778 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 776 def text_field(name, **attributes) input(**_attributes(:text, name, attributes)) end |
#time_field(name, **attributes) ⇒ String
Returns a time input tag.
513 514 515 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 513 def time_field(name, **attributes) input(**_attributes(:time, name, attributes)) end |
#url_field(name, **attributes) ⇒ String
Returns a URL input tag.
597 598 599 600 601 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 597 def url_field(name, **attributes) attributes[:value] = sanitize_url(attributes.fetch(:value) { _value(name) }) input(**_attributes(:url, name, attributes)) end |
#week_field(name, **attributes) ⇒ String
Returns a week input tag.
555 556 557 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 555 def week_field(name, **attributes) input(**_attributes(:week, name, attributes)) end |