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
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.
-
#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.
-
#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.
-
#url_field_tag(name, options = {}) ⇒ String
Creates a URL field input with the given name and 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.
510 511 512 |
# File 'lib/padrino-helpers/form_helpers.rb', line 510 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.
583 584 585 586 587 588 589 590 591 |
# File 'lib/padrino-helpers/form_helpers.rb', line 583 def (*args, &block) warn 'Warning: method button_to with block will change behavior on Padrino 0.13.0 release and will wrap the content of the block with <button></button> tag.' if block_given? = args..dup name, url = *args ['data-remote'] = 'true' if .delete(:remote) = .delete(:submit_options) || {} block ||= proc { submit_tag(name, ) } form_tag(url || name, , &block) end |
#check_box_tag(name, options = {}) ⇒ Object
Constructs a check_box from the given options.
423 424 425 |
# File 'lib/padrino-helpers/form_helpers.rb', line 423 def check_box_tag(name, ={}) input_tag(:checkbox, { :name => name, :value => '1' }.update()) end |
#email_field_tag(name, options = {}) ⇒ String
Creates an email field input with the given name and options.
343 344 345 |
# File 'lib/padrino-helpers/form_helpers.rb', line 343 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.
147 148 149 150 151 152 |
# File 'lib/padrino-helpers/form_helpers.rb', line 147 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.
71 72 73 74 75 76 |
# File 'lib/padrino-helpers/form_helpers.rb', line 71 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.
444 445 446 447 |
# File 'lib/padrino-helpers/form_helpers.rb', line 444 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 51 52 |
# 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() .delete(:as) .delete(:namespace) form_tag(url, ) { html } end |
#form_tag(url, options = {}, &block) ⇒ String
Constructs a form without object based on options.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/padrino-helpers/form_helpers.rb', line 93 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.
391 392 393 |
# File 'lib/padrino-helpers/form_helpers.rb', line 391 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’.
125 126 127 128 |
# File 'lib/padrino-helpers/form_helpers.rb', line 125 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.
548 549 550 |
# File 'lib/padrino-helpers/form_helpers.rb', line 548 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.
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/padrino-helpers/form_helpers.rb', line 172 def label_tag(name, ={}, &block) = { :caption => "#{name.to_s.humanize}: ", :for => name }.update() = ActiveSupport::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 |
#number_field_tag(name, options = {}) ⇒ String
Creates a number field input with the given name and options.
305 306 307 |
# File 'lib/padrino-helpers/form_helpers.rb', line 305 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.
413 414 415 |
# File 'lib/padrino-helpers/form_helpers.rb', line 413 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.
433 434 435 |
# File 'lib/padrino-helpers/form_helpers.rb', line 433 def (name, ={}) input_tag(:radio, { :name => name }.update()) end |
#range_field_tag(name, options = {}) ⇒ String
Constructs a range tag from the given options.
612 613 614 615 616 617 618 |
# File 'lib/padrino-helpers/form_helpers.rb', line 612 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.
365 366 367 |
# File 'lib/padrino-helpers/form_helpers.rb', line 365 def search_field_tag(name, ={}) input_tag(:search, { :name => name }.update()) end |
#select_tag(name, options = {}) ⇒ String
Constructs a select from the given options.
491 492 493 494 495 |
# File 'lib/padrino-helpers/form_helpers.rb', line 491 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.
529 530 531 532 533 |
# File 'lib/padrino-helpers/form_helpers.rb', line 529 def submit_tag(*args) = args. = 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" />
326 327 328 |
# File 'lib/padrino-helpers/form_helpers.rb', line 326 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.
401 402 403 404 |
# File 'lib/padrino-helpers/form_helpers.rb', line 401 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.
241 242 243 |
# File 'lib/padrino-helpers/form_helpers.rb', line 241 def text_field_tag(name, ={}) input_tag(:text, { :name => name }.update()) end |
#url_field_tag(name, options = {}) ⇒ String
Creates a URL field input with the given name and options.
381 382 383 |
# File 'lib/padrino-helpers/form_helpers.rb', line 381 def url_field_tag(name, ={}) input_tag(:url, { :name => name }.update()) end |