Module: Padrino::Helpers::FormHelpers
- Defined in:
- lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb
Overview
Helpers related to producing form related tags and inputs into templates.
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 = {}) ⇒ String
Constructs a check_box from the given options.
-
#csrf_token_field(token = nil) ⇒ String
Constructs a hidden field containing a CSRF token.
-
#email_field_tag(name, options = {}) ⇒ String
Creates an email field input with the given name and options.
-
#error_message_on(object, field, options = {}) ⇒ String
Returns a string containing the error message attached to the
method
on theobject
if one exists. -
#error_messages_for(*objects, options = {}) ⇒ String
Constructs list html for the errors for a given symbol.
-
#field_set_tag(*args, &block) ⇒ String
Constructs a field_set to group fields with given options.
-
#fields_for(object, settings = {}, &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.
-
#file_field_tag(name, options = {}) ⇒ String
Constructs a file field input from the given options.
-
#form_for(object, url, settings = {}, &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 = {}) ⇒ String
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 Only ‘get’ and ‘post’ are allowed within browsers; ‘put’ and ‘delete’ are just specified using hidden fields with form action still ‘put’.
-
#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 = {}) ⇒ String
Constructs a password field input from the given options.
-
#radio_button_tag(name, options = {}) ⇒ String
Constructs a radio_button 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(caption = "Submit", options = {}) ⇒ 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 = {}) ⇒ String
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.
Instance Method Details
#button_tag(caption, options = {}) ⇒ String
Constructs a button input from the given options
663 664 665 666 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 663 def (, = {}) .reverse_merge!(:value => ) input_tag(:button, ) end |
#button_to(name, url, options = {}) ⇒ String #button_to(name, options = {}, &block) ⇒ String
Creates a form containing a single button that submits to the url.
754 755 756 757 758 759 760 761 762 763 764 765 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 754 def (*args, &block) name, url = args[0], args[1] = args. ['data-remote'] = 'true' if .delete(:remote) if block_given? form_tag(url, , &block) else form_tag(url, ) do submit_tag(name) end end end |
#check_box_tag(name, options = {}) ⇒ String
Constructs a check_box from the given options
559 560 561 562 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 559 def check_box_tag(name, ={}) .reverse_merge!(:name => name, :value => '1') input_tag(:checkbox, ) end |
#csrf_token_field(token = nil) ⇒ String
Constructs a hidden field containing a CSRF token.
716 717 718 719 720 721 722 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 716 def csrf_token_field(token = nil) if defined? session token ||= (session[:csrf] ||= SecureRandom.hex(32)) end hidden_field_tag :authenticity_token, :value => token end |
#email_field_tag(name, options = {}) ⇒ String
Creates an email field input with the given name and options
464 465 466 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 464 def email_field_tag(name, ={}) input_tag(:email, .reverse_merge(:name => name)) end |
#error_message_on(object, field, options = {}) ⇒ String
Returns a string containing the error message attached to the method
on the object
if one exists.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 248 def (object, field, ={}) object = object.is_a?(Symbol) ? instance_variable_get("@#{object}") : object error = object.errors[field] rescue nil error = if defined?(Ohm::Model) && object.is_a?(Ohm::Model) I18n.t("ohm.errors.messages.#{error[0]}", :default => error[0].to_s) else # Array(error).first is necessary because some ORMs # give us an array others directly a value Array(error)[0] end if error = Array(error)[0] .reverse_merge!(:tag => :span, :class => :error) tag = .delete(:tag) error = [.delete(:prepend), error, .delete(:append)].compact.join(" ") content_tag(tag, error, ) else '' end end |
#error_messages_for(*objects, options = {}) ⇒ String
Constructs list html for the errors for a given symbol
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 169 def (*objects) = objects..symbolize_keys objects = objects.map { |object_name| object_name.is_a?(Symbol) ? instance_variable_get("@#{object_name}") : object_name }.compact count = objects.inject(0) { |sum, object| sum + object.errors.count } unless count.zero? html = {} [:id, :class, :style].each do |key| if .include?(key) value = [key] html[key] = value unless value.blank? else html[key] = 'field-errors' unless key == :style end end [:object_name] ||= objects.first.class I18n. :locale => [:locale], :scope => [:models, :errors, :template] do |locale| = if .include?(:header_message) [:header_message] else object_name = [:object_name].to_s.underscore.gsub(/\//, ' ') object_name = I18n.t(:name, :default => object_name.humanize, :scope => [:models, object_name], :count => 1) locale.t :header, :count => count, :model => object_name end = .include?(:message) ? [:message] : locale.t(:body) = objects.map { |object| object_name = [:object_name].to_s.underscore.gsub(/\//, ' ') object.errors.map { |f, msg| field = I18n.t(f, :default => f.to_s.humanize, :scope => [:models, object_name, :attributes]) content_tag(:li, "%s %s" % [field, msg]) } }.join contents = ActiveSupport::SafeBuffer.new contents << content_tag([:header_tag] || :h2, ) unless .blank? contents << content_tag(:p, ) unless .blank? contents << safe_content_tag(:ul, ) content_tag(:div, contents, html) end else '' end 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
129 130 131 132 133 134 135 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 129 def field_set_tag(*args, &block) = args. legend_text = args[0].is_a?(String) ? args.first : nil legend_html = legend_text.blank? ? ActiveSupport::SafeBuffer.new : content_tag(:legend, legend_text) field_set_content = legend_html + mark_safe(capture_html(&block)) concat_content content_tag(:fieldset, field_set_content, ) end |
#fields_for(object, settings = {}, &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
56 57 58 59 60 61 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 56 def fields_for(object, settings={}, &block) instance = builder_instance(object, settings) fields_html = capture_html(instance, &block) fields_html << instance.hidden_field(:id) if instance.send(:nested_object_id) concat_safe_content fields_html end |
#file_field_tag(name, options = {}) ⇒ String
Constructs a file field input from the given options
587 588 589 590 591 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 587 def file_field_tag(name, ={}) name = "#{name}[]" if [:multiple] .reverse_merge!(:name => name) input_tag(:file, ) end |
#form_for(object, url, settings = {}, &block) {|AbstractFormBuilder| ... } ⇒ String
Constructs a form for object using given or default form_builder
32 33 34 35 36 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 32 def form_for(object, url, settings={}, &block) instance = builder_instance(object, settings) html = capture_html(instance, &block) form_tag(url, settings) { html } end |
#form_tag(url, options = {}, &block) ⇒ String
Constructs a form without object based on options
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 79 def form_tag(url, ={}, &block) desired_method = [:method].to_s .delete(:method) unless desired_method =~ /get|post/i .reverse_merge!(:method => 'post', :action => url) [:enctype] = 'multipart/form-data' if .delete(:multipart) ['accept-charset'] ||= 'UTF-8' inner_form_html = hidden_form_method_field(desired_method) inner_form_html << csrf_token_field inner_form_html << mark_safe(capture_html(&block)) concat_content content_tag(:form, inner_form_html, ) end |
#hidden_field_tag(name, options = {}) ⇒ String
Constructs a hidden field input from the given options
517 518 519 520 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 517 def hidden_field_tag(name, ={}) .reverse_merge!(:name => name) input_tag(:hidden, ) 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’.
106 107 108 109 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 106 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
700 701 702 703 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 700 def image_submit_tag(source, ={}) .reverse_merge!(:src => image_path(source)) input_tag(:image, ) end |
#label_tag(name, options = {}, &block) ⇒ String
Constructs a label tag from the given options
288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 288 def label_tag(name, ={}, &block) .reverse_merge!(:caption => "#{name.to_s.humanize}: ", :for => name) = .delete(:caption).html_safe .safe_concat "<span class='required'>*</span> " if .delete(:required) if block_given? # label with inner content label_content = .concat capture_html(&block) concat_content(content_tag(:label, label_content, )) else # regular label content_tag(:label, , ) end end |
#number_field_tag(name, options = {}) ⇒ String
Creates a number field input with the given name and options
424 425 426 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 424 def number_field_tag(name, ={}) input_tag(:number, .reverse_merge(:name => name)) end |
#password_field_tag(name, options = {}) ⇒ String
Constructs a password field input from the given options
545 546 547 548 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 545 def password_field_tag(name, ={}) .reverse_merge!(:name => name) input_tag(:password, ) end |
#radio_button_tag(name, options = {}) ⇒ String
Constructs a radio_button from the given options
573 574 575 576 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 573 def (name, ={}) .reverse_merge!(:name => name) input_tag(:radio, ) end |
#search_field_tag(name, options = {}) ⇒ String
Creates a search field input with the given name and options
487 488 489 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 487 def search_field_tag(name, ={}) input_tag(:search, .reverse_merge(:name => name)) end |
#select_tag(name, options = {}) ⇒ String
Constructs a select from the given options
634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 634 def select_tag(name, ={}) .reverse_merge!(:name => name) collection, fields = .delete(:collection), .delete(:fields) [:options] = (collection, fields) if collection prompt = .delete(:include_blank) = if [:options] (.delete(:options), .delete(:selected)) elsif [:grouped_options] (.delete(:grouped_options), .delete(:selected), prompt) end = .unshift(blank_option(prompt)) if .is_a?(Array) .merge!(:name => "#{[:name]}[]") if [:multiple] safe_content_tag(:select, , ) end |
#submit_tag(caption = "Submit", options = {}) ⇒ String
Constructs a submit button from the given options
682 683 684 685 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 682 def submit_tag(="Submit", ={}) .reverse_merge!(:value => ) input_tag(:submit, ) 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">
446 447 448 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 446 def telephone_field_tag(name, ={}) input_tag(:tel, .reverse_merge(:name => name)) end |
#text_area_tag(name, options = {}) ⇒ String
Constructs a text area input from the given options
531 532 533 534 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 531 def text_area_tag(name, ={}) .reverse_merge!(:name => name, :rows => "", :cols => "") content_tag(:textarea, .delete(:value).to_s, ) end |
#text_field_tag(name, options = {}) ⇒ String
Creates a text field input with the given name and options
359 360 361 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 359 def text_field_tag(name, ={}) input_tag(:text, .reverse_merge!(:name => name)) end |
#url_field_tag(name, options = {}) ⇒ String
Creates a url field input with the given name and options
504 505 506 |
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 504 def url_field_tag(name, ={}) input_tag(:url, .reverse_merge(:name => name)) end |