Module: Padrino::Helpers::FormHelpers
- Defined in:
- lib/padrino-helpers/form_helpers.rb,
lib/padrino-helpers/form_helpers/errors.rb,
lib/padrino-helpers/form_helpers/options.rb,
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
Class Method Summary collapse
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.
Class Method Details
.included(base) ⇒ Object
11 12 13 14 15 |
# File 'lib/padrino-helpers/form_helpers.rb', line 11 def self.included(base) base.send(:include, FormHelpers::Errors) base.send(:include, FormHelpers::Options) base.send(:include, FormHelpers::Security) end |
Instance Method Details
#button_tag(caption, options = {}) ⇒ String
Constructs a button input from the given options.
508 509 510 |
# File 'lib/padrino-helpers/form_helpers.rb', line 508 def (caption, = {}) input_tag(:button, { :value => caption }.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.
581 582 583 584 585 586 587 588 589 590 591 592 593 |
# File 'lib/padrino-helpers/form_helpers.rb', line 581 def (*args, &block) = args..dup 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.
421 422 423 |
# File 'lib/padrino-helpers/form_helpers.rb', line 421 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.
795 796 797 798 799 |
# File 'lib/padrino-helpers/form_helpers.rb', line 795 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.
697 698 699 700 701 |
# File 'lib/padrino-helpers/form_helpers.rb', line 697 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.
645 646 647 648 649 |
# File 'lib/padrino-helpers/form_helpers.rb', line 645 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.
671 672 673 674 675 |
# File 'lib/padrino-helpers/form_helpers.rb', line 671 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.
341 342 343 |
# File 'lib/padrino-helpers/form_helpers.rb', line 341 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 150 |
# File 'lib/padrino-helpers/form_helpers.rb', line 145 def field_set_tag(*args, &block) = args. legend_text = args.first legend_html = legend_text.blank? ? ActiveSupport::SafeBuffer.new : content_tag(:legend, legend_text) 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 '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.
442 443 444 445 |
# File 'lib/padrino-helpers/form_helpers.rb', line 442 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 '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 'lib/padrino-helpers/form_helpers.rb', line 91 def form_tag(url, ={}, &block) = { :action => 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.
389 390 391 |
# File 'lib/padrino-helpers/form_helpers.rb', line 389 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 'lib/padrino-helpers/form_helpers.rb', line 123 def hidden_form_method_field(desired_method) return ActiveSupport::SafeBuffer.new if desired_method.blank? || 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.
546 547 548 |
# File 'lib/padrino-helpers/form_helpers.rb', line 546 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.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/padrino-helpers/form_helpers.rb', line 170 def label_tag(name, ={}, &block) = { :caption => "#{name.to_s.humanize}: ", :for => name }.update() caption_text = ActiveSupport::SafeBuffer.new << .delete(:caption) caption_text << "<span class='required'>*</span> ".html_safe if .delete(:required) if block_given? concat_content content_tag(:label, caption_text << capture_html(&block), ) else content_tag(:label, caption_text, ) end end |
#month_field_tag(name, options = {}) ⇒ String
Constructs a month tag from the given options.
723 724 725 726 727 |
# File 'lib/padrino-helpers/form_helpers.rb', line 723 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.
303 304 305 |
# File 'lib/padrino-helpers/form_helpers.rb', line 303 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.
411 412 413 |
# File 'lib/padrino-helpers/form_helpers.rb', line 411 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.
431 432 433 |
# File 'lib/padrino-helpers/form_helpers.rb', line 431 def (name, ={}) input_tag(:radio, { :name => name }.update()) end |
#range_field_tag(name, options = {}) ⇒ String
Constructs a range tag from the given options.
614 615 616 617 618 619 620 |
# File 'lib/padrino-helpers/form_helpers.rb', line 614 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.
363 364 365 |
# File 'lib/padrino-helpers/form_helpers.rb', line 363 def search_field_tag(name, ={}) input_tag(:search, { :name => name }.update()) end |
#select_tag(name, options = {}) ⇒ String
Constructs a select from the given options.
489 490 491 492 493 |
# File 'lib/padrino-helpers/form_helpers.rb', line 489 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.
527 528 529 530 531 |
# File 'lib/padrino-helpers/form_helpers.rb', line 527 def submit_tag(*args) = args. caption = args.length >= 1 ? args.first : "Submit" input_tag(:submit, { :value => caption }.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" />
324 325 326 |
# File 'lib/padrino-helpers/form_helpers.rb', line 324 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.
399 400 401 402 |
# File 'lib/padrino-helpers/form_helpers.rb', line 399 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.
239 240 241 |
# File 'lib/padrino-helpers/form_helpers.rb', line 239 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.
775 776 777 778 779 |
# File 'lib/padrino-helpers/form_helpers.rb', line 775 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.
379 380 381 |
# File 'lib/padrino-helpers/form_helpers.rb', line 379 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.
749 750 751 752 753 |
# File 'lib/padrino-helpers/form_helpers.rb', line 749 def week_field_tag(name, = {}) = { :name => name }.update() = convert_attributes_into_datetime("%Y-W%W", ) input_tag(:week, ) end |