Module: Padrino::Helpers::FormHelpers
- Defined in:
- padrino-helpers/lib/padrino-helpers/form_helpers.rb,
padrino-helpers/lib/padrino-helpers/form_helpers/errors.rb,
padrino-helpers/lib/padrino-helpers/form_helpers/options.rb,
padrino-helpers/lib/padrino-helpers/form_helpers/security.rb
Overview
Helpers related to producing form related tags and inputs into templates.
Defined Under Namespace
Modules: Errors, Options, Security
Constant Summary collapse
- DATETIME_ATTRIBUTES =
[:value, :max, :min].freeze
- COLOR_CODE_REGEXP =
/\A#([0-9a-fA-F]{3}){1,2}\z/.freeze
Instance Method Summary collapse
-
#button_tag(caption, options = {}) ⇒ String
Constructs a button input from the given options.
-
#button_to(*args, &block) ⇒ String
Creates a form containing a single button that submits to the URL.
-
#check_box_tag(name, options = {}) ⇒ Object
Constructs a check_box from the given options.
-
#color_field_tag(name, options = {}) ⇒ Object
Constructs a color tag from the given options.
-
#date_field_tag(name, options = {}) ⇒ String
Constructs a date tag from the given options.
-
#datetime_field_tag(name, options = {}) ⇒ String
Constructs a datetime tag from the given options.
-
#datetime_local_field_tag(name, options = {}) ⇒ String
Constructs a datetime-local tag from the given options.
-
#email_field_tag(name, options = {}) ⇒ String
Creates an email field input with the given name and options.
-
#field_set_tag(*args, &block) ⇒ String
Constructs a field_set to group fields with given options.
-
#fields_for(object, options = {}, &block) ⇒ String
Constructs form fields for an object using given or default form_builder.
-
#file_field_tag(name, options = {}) ⇒ Object
Constructs a file field input from the given options.
-
#form_for(object, url, options = {}, &block) {|AbstractFormBuilder| ... } ⇒ String
Constructs a form for object using given or default form_builder.
-
#form_tag(url, options = {}, &block) ⇒ String
Constructs a form without object based on options.
-
#hidden_field_tag(name, options = {}) ⇒ Object
Constructs a hidden field input from the given options.
-
#hidden_form_method_field(desired_method) ⇒ String
Returns the hidden method field for ‘put’ and ‘delete’ forms.
-
#image_submit_tag(source, options = {}) ⇒ String
Constructs a submit button from the given options.
-
#label_tag(name, options = {}, &block) ⇒ String
Constructs a label tag from the given options.
-
#month_field_tag(name, options = {}) ⇒ String
Constructs a month tag from the given options.
-
#number_field_tag(name, options = {}) ⇒ String
Creates a number field input with the given name and options.
-
#password_field_tag(name, options = {}) ⇒ Object
Constructs a password field input from the given options.
-
#radio_button_tag(name, options = {}) ⇒ Object
Constructs a radio_button from the given options.
-
#range_field_tag(name, options = {}) ⇒ String
Constructs a range tag from the given options.
-
#search_field_tag(name, options = {}) ⇒ String
Creates a search field input with the given name and options.
-
#select_tag(name, options = {}) ⇒ String
Constructs a select from the given options.
-
#submit_tag(*args) ⇒ String
Constructs a submit button from the given options.
-
#telephone_field_tag(name, options = {}) ⇒ String
(also: #phone_field_tag)
Creates a telephone field input with the given name and options.
-
#text_area_tag(name, options = {}) ⇒ Object
Constructs a text area input from the given options.
-
#text_field_tag(name, options = {}) ⇒ String
Creates a text field input with the given name and options.
-
#time_field_tag(name, options = {}) ⇒ String
Constructs a time tag from the given options.
-
#url_field_tag(name, options = {}) ⇒ String
Creates a URL field input with the given name and options.
-
#week_field_tag(name, options = {}) ⇒ String
Constructs a week tag from the given options.
Instance Method Details
#button_tag(caption, options = {}) ⇒ String
Constructs a button input from the given options.
507 508 509 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 507 def (, = {}) input_tag(:button, { :value => }.update()) end |
#button_to(caption, url, options = {}) ⇒ String #button_to(url, options = {}, &block) ⇒ String
Creates a form containing a single button that submits to the URL.
580 581 582 583 584 585 586 587 588 589 590 591 592 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 580 def (*args, &block) = args.last.is_a?(Hash) ? args.pop : {} name, url = *args ['data-remote'] = 'true' if .delete(:remote) = .delete(:submit_options) || {} form_tag(url || name, ) do if block_given? content_tag(:button, capture_html(&block), ) else submit_tag(name, ) end end end |
#check_box_tag(name, options = {}) ⇒ Object
Constructs a check_box from the given options.
420 421 422 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 420 def check_box_tag(name, ={}) input_tag(:checkbox, { :name => name, :value => '1' }.update()) end |
#color_field_tag(name, options = {}) ⇒ Object
Constructs a color tag from the given options.
794 795 796 797 798 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 794 def color_field_tag(name, = {}) = { :name => name }.update() [:value] = adjust_color([:value]) input_tag(:color, ) end |
#date_field_tag(name, options = {}) ⇒ String
Constructs a date tag from the given options.
696 697 698 699 700 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 696 def date_field_tag(name, = {}) = { :name => name }.update() = convert_attributes_into_datetime("%Y-%m-%d", ) input_tag(:date, ) end |
#datetime_field_tag(name, options = {}) ⇒ String
Constructs a datetime tag from the given options.
644 645 646 647 648 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 644 def datetime_field_tag(name, = {}) = { :name => name }.update() = convert_attributes_into_datetime("%Y-%m-%dT%T.%L%z", ) input_tag(:datetime, ) end |
#datetime_local_field_tag(name, options = {}) ⇒ String
Constructs a datetime-local tag from the given options.
670 671 672 673 674 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 670 def datetime_local_field_tag(name, = {}) = { :name => name }.update() = convert_attributes_into_datetime("%Y-%m-%dT%T", ) input_tag(:"datetime-local", ) end |
#email_field_tag(name, options = {}) ⇒ String
Creates an email field input with the given name and options.
340 341 342 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 340 def email_field_tag(name, ={}) input_tag(:email, { :name => name }.update()) end |
#field_set_tag(legend = nil, options = {}, &block) ⇒ String #field_set_tag(options = {}, &block) ⇒ String
Constructs a field_set to group fields with given options.
145 146 147 148 149 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 145 def field_set_tag(*args, &block) = args.last.is_a?(Hash) ? args.pop : {} legend_html = args.empty? ? SafeBuffer.new : content_tag(:legend, args.first) concat_content content_tag(:fieldset, legend_html << capture_html(&block), ) end |
#fields_for(object, options = {}, &block) ⇒ String
Constructs form fields for an object using given or default form_builder. Used within an existing form to allow alternate objects within one form.
69 70 71 72 73 74 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 69 def fields_for(object, ={}, &block) instance = builder_instance(object, ) fields_html = capture_html(instance, &block) fields_html << instance.hidden_field(:id) if instance.send(:nested_object_id) concat_content fields_html end |
#file_field_tag(name, options = {}) ⇒ Object
Constructs a file field input from the given options.
441 442 443 444 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 441 def file_field_tag(name, ={}) name = "#{name}[]" if [:multiple] input_tag(:file, { :name => name }.update()) end |
#form_for(object, url, options = {}, &block) {|AbstractFormBuilder| ... } ⇒ String
Constructs a form for object using given or default form_builder.
44 45 46 47 48 49 50 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 44 def form_for(object, url, ={}, &block) instance = builder_instance(object, ) # this can erect instance.multipart flag if the block calls instance.file_field html = capture_html(instance, &block) = { :multipart => instance.multipart }.update(.reject{ |key,_| [:namespace, :as].include?(key) }) form_tag(url, ) { html } end |
#form_tag(url, options = {}, &block) ⇒ String
Constructs a form without object based on options.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 91 def form_tag(url, ={}, &block) = { :action => escape_link(url), :protect_from_csrf => is_protected_from_csrf?, 'accept-charset' => 'UTF-8' }.update() [:enctype] = 'multipart/form-data' if .delete(:multipart) if (desired_method = [:method]) =~ /get/i .delete(:protect_from_csrf) else [:method] = 'post' end inner_form_html = hidden_form_method_field(desired_method) inner_form_html << csrf_token_field if .delete(:protect_from_csrf) concat_content content_tag(:form, inner_form_html << capture_html(&block), ) end |
#hidden_field_tag(name, options = {}) ⇒ Object
Constructs a hidden field input from the given options.
388 389 390 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 388 def hidden_field_tag(name, ={}) input_tag(:hidden, { :name => name }.update()) end |
#hidden_form_method_field(desired_method) ⇒ String
Returns the hidden method field for ‘put’ and ‘delete’ forms. Only ‘get’ and ‘post’ are allowed within browsers; ‘put’ and ‘delete’ are just specified using hidden fields with form action still ‘put’.
123 124 125 126 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 123 def hidden_form_method_field(desired_method) return SafeBuffer.new if desired_method.nil? || desired_method.to_s =~ /get|post/i hidden_field_tag(:_method, :value => desired_method) end |
#image_submit_tag(source, options = {}) ⇒ String
Constructs a submit button from the given options.
545 546 547 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 545 def image_submit_tag(source, ={}) input_tag(:image, { :src => image_path(source) }.update()) end |
#label_tag(name, options = {}, &block) ⇒ String
Constructs a label tag from the given options.
169 170 171 172 173 174 175 176 177 178 179 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 169 def label_tag(name, ={}, &block) = { :caption => "#{Inflections.humanize(name)}: ", :for => name }.update() = SafeBuffer.new << .delete(:caption) << "<span class='required'>*</span> ".html_safe if .delete(:required) if block_given? concat_content content_tag(:label, << capture_html(&block), ) else content_tag(:label, , ) end end |
#month_field_tag(name, options = {}) ⇒ String
Constructs a month tag from the given options.
722 723 724 725 726 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 722 def month_field_tag(name, = {}) = { :name => name }.update() = convert_attributes_into_datetime("%Y-%m", ) input_tag(:month, ) end |
#number_field_tag(name, options = {}) ⇒ String
Creates a number field input with the given name and options.
302 303 304 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 302 def number_field_tag(name, ={}) input_tag(:number, { :name => name }.update()) end |
#password_field_tag(name, options = {}) ⇒ Object
Constructs a password field input from the given options.
410 411 412 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 410 def password_field_tag(name, ={}) input_tag(:password, { :name => name }.update()) end |
#radio_button_tag(name, options = {}) ⇒ Object
Constructs a radio_button from the given options.
430 431 432 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 430 def (name, ={}) input_tag(:radio, { :name => name }.update()) end |
#range_field_tag(name, options = {}) ⇒ String
Constructs a range tag from the given options.
613 614 615 616 617 618 619 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 613 def range_field_tag(name, = {}) = { :name => name }.update() if range = .delete(:range) [:min], [:max] = range.min, range.max end input_tag(:range, ) end |
#search_field_tag(name, options = {}) ⇒ String
Creates a search field input with the given name and options.
362 363 364 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 362 def search_field_tag(name, ={}) input_tag(:search, { :name => name }.update()) end |
#select_tag(name, options = {}) ⇒ String
Constructs a select from the given options.
488 489 490 491 492 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 488 def select_tag(name, ={}) = { :name => name }.merge() [:name] = "#{[:name]}[]" if [:multiple] content_tag(:select, (), ) end |
#submit_tag(options = {}) ⇒ String #submit_tag(caption, options = {}) ⇒ String
Constructs a submit button from the given options.
526 527 528 529 530 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 526 def submit_tag(*args) = args.last.is_a?(Hash) ? args.pop : {} = args.length >= 1 ? args.first : "Submit" input_tag(:submit, { :value => }.merge()) end |
#telephone_field_tag(name, options = {}) ⇒ String Also known as: phone_field_tag
Creates a telephone field input with the given name and options.
telephone_field_tag :cell_phone, :tabindex => 1
telephone_field_tag :work_phone, :tabindex => 2
telephone_field_tag :home_phone, :tabindex => 3
# => <input name="cell_phone" tabindex="1" type="tel" />
# => <input name="work_phone" tabindex="2" type="tel" />
# => <input name="home_phone" tabindex="3" type="tel" />
323 324 325 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 323 def telephone_field_tag(name, ={}) input_tag(:tel, { :name => name }.update()) end |
#text_area_tag(name, options = {}) ⇒ Object
Constructs a text area input from the given options.
398 399 400 401 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 398 def text_area_tag(name, ={}) inner_html = TagHelpers::NEWLINE + .delete(:value).to_s content_tag(:textarea, inner_html, { :name => name }.update()) end |
#text_field_tag(name, options = {}) ⇒ String
Creates a text field input with the given name and options.
238 239 240 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 238 def text_field_tag(name, ={}) input_tag(:text, { :name => name }.update()) end |
#time_field_tag(name, options = {}) ⇒ String
Constructs a time tag from the given options.
774 775 776 777 778 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 774 def time_field_tag(name, = {}) = { :name => name }.update() = convert_attributes_into_datetime("%T.%L", ) input_tag(:time, ) end |
#url_field_tag(name, options = {}) ⇒ String
Creates a URL field input with the given name and options.
378 379 380 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 378 def url_field_tag(name, ={}) input_tag(:url, { :name => name }.update()) end |
#week_field_tag(name, options = {}) ⇒ String
Constructs a week tag from the given options.
748 749 750 751 752 |
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 748 def week_field_tag(name, = {}) = { :name => name }.update() = convert_attributes_into_datetime("%Y-W%W", ) input_tag(:week, ) end |