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.
1125 1126 1127 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1125 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.
407 408 409 410 411 412 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 407 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.
431 432 433 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 431 def color_field(name, **attributes) input(**_attributes(:color, name, attributes)) end |
#datalist(name, values, list, **attributes) ⇒ String
Returns a datalist input tag.
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1070 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.
452 453 454 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 452 def date_field(name, **attributes) input(**_attributes(:date, name, attributes)) end |
#datetime_field(name, **attributes) ⇒ String
Returns a datetime input tag.
473 474 475 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 473 def datetime_field(name, **attributes) input(**_attributes(:datetime, name, attributes)) end |
#datetime_local_field(name, **attributes) ⇒ String
Returns a datetime-local input tag.
494 495 496 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 494 def datetime_local_field(name, **attributes) input(**_attributes(:"datetime-local", name, attributes)) end |
#email_field(name, **attributes) ⇒ String
Returns an email input tag.
578 579 580 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 578 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 233 234 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 228 def fields_for_collection(name, &block) collection_base_name = [base_name, name.to_s].compact.join(INPUT_NAME_SEPARATOR) _value(name).each_with_index do |value, index| fields_for("#{collection_base_name}.#{index}", index, value, &block) end end |
#fieldset(**attributes, &block) ⇒ String
328 329 330 331 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 328 def fieldset(...) # This is here only for documentation purposes tag.fieldset(...) end |
#file_field(name, **attributes) ⇒ String
Returns a file input tag.
673 674 675 676 677 678 679 680 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 673 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.
639 640 641 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 639 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.
1148 1149 1150 1151 1152 1153 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1148 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.
1221 1222 1223 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1221 def input(...) tag.input(...) end |
#label(field_name, **attributes) ⇒ String #label(content, **attributes) ⇒ String #label(field_name, **attributes, &block) ⇒ String
Returns a label tag.
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 292 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.split(INPUT_NAME_SEPARATOR).last) end tag.label(content, **attributes, &block) end |
#month_field(name, **attributes) ⇒ String
Returns a month input tag.
536 537 538 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 536 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.
701 702 703 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 701 def number_field(name, **attributes) input(**_attributes(:number, name, attributes)) end |
#password_field(name, **attributes) ⇒ String
Returns a password input tag.
863 864 865 866 867 868 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 863 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.
843 844 845 846 847 848 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 843 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.
724 725 726 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 724 def range_field(name, **attributes) input(**_attributes(:range, name, attributes)) end |
#search_field(name, **attributes) ⇒ String
Returns a search input tag.
800 801 802 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 800 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.
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 991 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, disabled: true) 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.
1192 1193 1194 1195 1196 1197 1198 1199 1200 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 1192 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.
622 623 624 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 622 def tel_field(name, **attributes) input(**_attributes(:tel, name, attributes)) end |
#text_area(name, content = nil, **attributes) ⇒ String
Returns a textarea tag.
751 752 753 754 755 756 757 758 759 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 751 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.
778 779 780 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 778 def text_field(name, **attributes) input(**_attributes(:text, name, attributes)) end |
#time_field(name, **attributes) ⇒ String
Returns a time input tag.
515 516 517 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 515 def time_field(name, **attributes) input(**_attributes(:time, name, attributes)) end |
#url_field(name, **attributes) ⇒ String
Returns a URL input tag.
599 600 601 602 603 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 599 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.
557 558 559 |
# File 'lib/hanami/helpers/form_helper/form_builder.rb', line 557 def week_field(name, **attributes) input(**_attributes(:week, name, attributes)) end |