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

Instance Method Details

#button_tag(caption, options = {}) ⇒ String

Constructs a button input from the given options

Examples:

button_tag "Cancel", :class => 'clear'

Parameters:

  • caption (String)

    The caption for the button.

  • options (Hash) (defaults to: {})

    The html options for the input field.

Returns:

  • (String)

    The html button based on the options specified.



663
664
665
666
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 663

def button_tag(caption, options = {})
  options.reverse_merge!(:value => caption)
  input_tag(:button, options)
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.

Examples:

button_to 'Delete', url(:accounts_destroy, :id => ), :method => :delete, :class => :form
# Generates:
# <form class="form" action="/admin/accounts/destroy/2" method="post">
#   <input type="hidden" value="delete" name="_method" />
#   <input type="submit" value="Delete" />
# </form>

Overloads:

  • #button_to(name, url, options = {}) ⇒ String

    Parameters:

    • caption (String)

      The text caption.

    • url (String)

      The url href.

    • options (Hash) (defaults to: {})

      The html options.

  • #button_to(name, options = {}, &block) ⇒ String

    Parameters:

    • url (String)

      The url href.

    • options (Hash) (defaults to: {})

      The html options.

    • block (Proc)

      The button content.

Parameters:

  • options (Hash)

    a customizable set of options

Returns:

  • (String)

    Form and button html with specified options.



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 button_to(*args, &block)
  name, url = args[0], args[1]
  options   = args.extract_options!
  options['data-remote'] = 'true' if options.delete(:remote)
  if block_given?
    form_tag(url, options, &block)
  else
    form_tag(url, options) do
      submit_tag(name)
    end
  end
end

#check_box_tag(name, options = {}) ⇒ String

Constructs a check_box from the given options

Examples:

check_box_tag :remember_me, :value => 'Yes'

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  options.reverse_merge!(:name => name, :value => '1')
  input_tag(:checkbox, options)
end

#csrf_token_field(token = nil) ⇒ String

Constructs a hidden field containing a CSRF token.

Examples:

csrf_token_field

Parameters:

  • token (String) (defaults to: nil)

    The token to use. Will be read from the session by default.

Returns:

  • (String)

    The hidden field with CSRF token as value.



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

Examples:

email_field_tag :email, :placeholder => '[email protected]'
# => <input name="email" placeholder="[email protected]" type="email">

email_field_tag :email, :value => '[email protected]', :readonly => true
# => <input name="email" value="[email protected]" readonly type="email">

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  input_tag(:email, options.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.

Examples:

# => <span class="error">can't be blank</div>
error_message_on :post, :title
error_message_on @post, :title

# => <div class="custom" style="border:1px solid red">can't be blank</div>
error_message_on :post, :title, :tag => :id, :class => :custom, :style => "border:1px solid red"

# => <div class="error">This title can't be blank (or it won't work)</div>
error_message_on :post, :title, :prepend => "This title", :append => "(or it won't work)"

Parameters:

  • object (Object)

    The object to display the error for.

  • field (Symbol)

    The field on the object to display the error for.

  • options (Hash) (defaults to: {})

    The options to control the error display.

Options Hash (options):

  • :tag (String) — default: "span"

    The tag that encloses the error.

  • :prepend (String) — default: ""

    The text to prepend before the field error.

  • :append (String) — default: ""

    The text to append after the field error.

Returns:

  • (String)

    The html display of an error for a particular object and field.



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 error_message_on(object, field, options={})
  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]
    options.reverse_merge!(:tag => :span, :class => :error)
    tag   = options.delete(:tag)
    error = [options.delete(:prepend), error, options.delete(:append)].compact.join(" ")
    (tag, error, options)
  else
    ''
  end
end

#error_messages_for(*objects, options = {}) ⇒ String

Constructs list html for the errors for a given symbol

Examples:

error_messages_for :user

Parameters:

  • object (Array<Object>)

    Splat of objects to display errors for.

  • options (Hash) (defaults to: {})

    Error message display options.

Options Hash (options):

  • :header_tag (String) — default: "h2"

    Used for the header of the error div

  • :id (String) — default: "field-errors"

    The id of the error div.

  • :class (String) — default: "field-errors"

    The class of the error div.

  • :object (Array<Object>)

    The object (or array of objects) for which to display errors, if you need to escape the instance variable convention.

  • :object_name (String)

    The object name to use in the header, or any text that you prefer. If :object_name is not set, the name of the first object will be used.

  • :header_message (String) — default: "X errors prohibited this object from being saved"

    The message in the header of the error div. Pass nil or an empty string to avoid the header message altogether.

  • :message (String) — default: "There were problems with the following fields:"

    The explanation message after the header message and before the error list. Pass nil or an empty string to avoid the explanation message altogether.

Returns:

  • (String)

    The html section with all errors for the specified objects



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 error_messages_for(*objects)
  options = objects.extract_options!.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 options.include?(key)
        value = options[key]
        html[key] = value unless value.blank?
      else
        html[key] = 'field-errors' unless key == :style
      end
    end

    options[:object_name] ||= objects.first.class

    I18n.with_options :locale => options[:locale], :scope => [:models, :errors, :template] do |locale|
      header_message = if options.include?(:header_message)
        options[:header_message]
      else
        object_name = options[: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
      message = options.include?(:message) ? options[:message] : locale.t(:body)
      error_messages = objects.map { |object|
        object_name = options[: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])
          (:li, "%s %s" % [field, msg])
        }
      }.join

      contents = ActiveSupport::SafeBuffer.new
      contents << (options[:header_tag] || :h2, header_message) unless header_message.blank?
      contents << (:p, message) unless message.blank?
      contents << (:ul, error_messages)

      (: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

Examples:

field_set_tag(:class => "office-set") { }
field_set_tag("Office", :class => 'office-set') { }

Overloads:

  • #field_set_tag(legend = nil, options = {}, &block) ⇒ String

    Parameters:

    • legend (String) (defaults to: nil)

      The legend caption for the fieldset

    • options (Hash) (defaults to: {})

      The html options for the fieldset.

    • block (Proc)

      The content inside the fieldset.

  • #field_set_tag(options = {}, &block) ⇒ String

    Parameters:

    • options (Hash) (defaults to: {})

      The html options for the fieldset.

    • block (Proc)

      The content inside the fieldset.

Returns:

  • (String)

    The html for the fieldset tag based on 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)
  options = args.extract_options!
  legend_text = args[0].is_a?(String) ? args.first : nil
  legend_html = legend_text.blank? ? ActiveSupport::SafeBuffer.new : (:legend, legend_text)
  field_set_content = legend_html + mark_safe(capture_html(&block))
  concat_content (:fieldset, field_set_content, options)
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

Examples:

fields_for @user.assignment do |assignment| ... end
fields_for :assignment do |assigment| ... end

Parameters:

  • object (Object)

    The object for which the fields are being built.

  • settings (Hash) (defaults to: {})

    The settings associated with these fields. Accepts html options.

  • block (Proc)

    The content inside this set of fields.

Returns:

  • (String)

    The html fields with the specified options.



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

Examples:

file_field_tag :photo, :class => 'long'

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  name = "#{name}[]" if options[:multiple]
  options.reverse_merge!(:name => name)
  input_tag(:file, options)
end

#form_for(object, url, settings = {}, &block) {|AbstractFormBuilder| ... } ⇒ String

Constructs a form for object using given or default form_builder

Examples:

form_for :user, '/register' do |f| ... end
form_for @user, '/register', :id => 'register' do |f| ... end

Parameters:

  • object (Object)

    The object for which the form is being built.

  • url (String)

    The url this form will submit to.

  • settings (Hash) (defaults to: {})

    The settings associated with this form. Accepts html options.

  • block (Proc)

    The fields and content inside this form.

Options Hash (settings):

  • :builder (String) — default: "StandardFormBuilder"

    The FormBuilder class to use such as StandardFormBuilder.

Yields:

  • (AbstractFormBuilder)

    The form builder used to compose fields.

Returns:

  • (String)

    The html object-backed form with the specified options and input fields.



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

Examples:

form_tag '/register', :class => "registration_form" do ... end

Parameters:

  • url (String)

    The url this form will submit to.

  • options (Hash) (defaults to: {})

    The html options associated with this form.

  • block (Proc)

    The fields and content inside this form.

Returns:

  • (String)

    The html form with the specified options and input fields.



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, options={}, &block)
  desired_method = options[:method].to_s
  options.delete(:method) unless desired_method =~ /get|post/i
  options.reverse_merge!(:method => 'post', :action => url)
  options[:enctype] = 'multipart/form-data' if options.delete(:multipart)
  options['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 (:form, inner_form_html, options)
end

#hidden_field_tag(name, options = {}) ⇒ String

Constructs a hidden field input from the given options

Examples:

hidden_field_tag :session_key, :value => "__secret__"

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  options.reverse_merge!(:name => name)
  input_tag(:hidden, options)
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’.

Examples:

# Generate: <input name="_method" value="delete" />
hidden_form_method_field('delete')

Parameters:

  • desired_method (String)

    The method this hidden field represents (i.e put or delete))

Returns:

  • (String)

    The hidden field representing the desired_method for the form.



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

Examples:

submit_tag "Create", :class => 'success'

Parameters:

  • source (String)

    The source image path for the button.

  • options (Hash) (defaults to: {})

    The html options for the input field.

Returns:

  • (String)

    The html image button based on the options specified.



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, options={})
  options.reverse_merge!(:src => image_path(source))
  input_tag(:image, options)
end

#label_tag(name, options = {}, &block) ⇒ String

Constructs a label tag from the given options

Examples:

label_tag :username, :class => 'long-label'
label_tag :username, :class => 'long-label' do ... end

Parameters:

  • name (String)

    The name of the field to label.

  • options (Hash) (defaults to: {})

    The html options for this label.

  • block (Proc)

    The content to be inserted into the label.

Options Hash (options):

  • :caption (Object)

    The caption for this label.

Returns:

  • (String)

    The html for this label with 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, options={}, &block)
  options.reverse_merge!(:caption => "#{name.to_s.humanize}: ", :for => name)
  caption_text = options.delete(:caption).html_safe
  caption_text.safe_concat "<span class='required'>*</span> " if options.delete(:required)

  if block_given? # label with inner content
    label_content = caption_text.concat capture_html(&block)
    concat_content((:label, label_content, options))
  else # regular label
    (:label, caption_text, options)
  end
end

#number_field_tag(name, options = {}) ⇒ String

Creates a number field input with the given name and options

Examples:

number_field_tag :quanity, :class => 'numeric'
# => <input name="quanity" class="numeric" type="number">

number_field_tag :zip_code, :pattern => /[0-9]{5}/
# => <input name="zip_code" pattern="[0-9]{5}" type="number">

number_field_tag :credit_card, :autocomplete => :off
# => <input name="credit_card" autocomplete="off" type="number">

number_field_tag :age, :min => 18, :max => 120, :step => 1
# => <input name="age" min="18" max="120" step="1" type="number">

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :min (Integer)

    Specifies the minimum value of the field.

  • :max (Integer)

    Specifies the maximum value of the field.

  • :step (Integer)

    Specifies the legal number intervals of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  input_tag(:number, options.reverse_merge(:name => name))
end

#password_field_tag(name, options = {}) ⇒ String

Constructs a password field input from the given options

Examples:

password_field_tag :password, :class => 'long'

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  options.reverse_merge!(:name => name)
  input_tag(:password, options)
end

#radio_button_tag(name, options = {}) ⇒ String

Constructs a radio_button from the given options

Examples:

radio_button_tag :remember_me, :value => 'true'

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified options



573
574
575
576
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/form_helpers.rb', line 573

def radio_button_tag(name, options={})
  options.reverse_merge!(:name => name)
  input_tag(:radio, options)
end

#search_field_tag(name, options = {}) ⇒ String

Creates a search field input with the given name and options

Examples:

search_field_tag :search, :placeholder => 'Search this website...'
# => <input name="search" placeholder="Search this website..." type="search">

search_field_tag :search, :maxlength => 15, :class => ['search', 'string']
# => <input name="search" maxlength="15" class="search string">

search_field_tag :search, :id => 'search'
# => <input name="search" id="search" type="search">

search_field_tag :search, :autofocus => true
# => <input name="search" autofocus type="search">

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  input_tag(:search, options.reverse_merge(:name => name))
end

#select_tag(name, options = {}) ⇒ String

Constructs a select from the given options

Examples:

options = [['caption', 'value'], ['Green', 'green1'], ['Blue', 'blue1'], ['Black', "black1"]]
options = ['option', 'red', 'yellow' ]
select_tag(:favorite_color, :options => ['red', 'yellow'], :selected => 'green1')
select_tag(:country, :collection => @countries, :fields => [:name, :code], :include_blank => 'None')

# Optgroups can be generated using :grouped_options => (Hash or nested Array)
grouped_options = [['Friends',['Yoda',['Obiwan',1]]],['Enemies',['Palpatine',['Darth Vader',3]]]]
grouped_options = {'Friends' => ['Yoda',['Obiwan',1]],'Enemies' => ['Palpatine',['Darth Vader',3]]}
select_tag(:color, :grouped_options => [['warm',['red','yellow']],['cool',['blue', 'purple']]])

# Optgroups can be generated using :grouped_options => (Hash or nested Array)
grouped_options = [['Friends',['Yoda',['Obiwan',1]]],['Enemies',['Palpatine',['Darth Vader',3]]]]
grouped_options = {'Friends' => ['Yoda',['Obiwan',1]],'Enemies' => ['Palpatine',['Darth Vader',3]]}
select_tag(:color, :grouped_options => [['warm',['red','yellow']],['cool',['blue', 'purple']]])

Parameters:

  • name (String)

    The name of the input field.

  • options (Hash) (defaults to: {})

    The html options for the input field.

Options Hash (options):

  • :options (Array<String, Array>)

    Explicit options to display in the select. Can be strings or string tuples.

  • :grouped_options (Array<Array>)

    List of options for each group in the select. See examples for details.

  • :collection (Array<Object>)

    Collection of objects used as options in the select.

  • :fields (Array<Symbol>)

    The attributes used as “label” and “value” for each collection object.

  • :selected (String) — default: nil

    The option value initially selected.

  • :include_blank (Boolean) — default: false

    Include a blank option in the select.

  • :multiple (Boolean) — default: false

    Allow multiple options to be selected at once.

Returns:

  • (String)

    The html input field based on the options specified



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, options={})
  options.reverse_merge!(:name => name)
  collection, fields = options.delete(:collection), options.delete(:fields)
  options[:options] = options_from_collection(collection, fields) if collection
  prompt = options.delete(:include_blank)
  select_options_html = if options[:options]
    options_for_select(options.delete(:options), options.delete(:selected))
  elsif options[:grouped_options]
    grouped_options_for_select(options.delete(:grouped_options), options.delete(:selected), prompt)
  end
  select_options_html = select_options_html.unshift(blank_option(prompt)) if select_options_html.is_a?(Array)
  options.merge!(:name => "#{options[:name]}[]") if options[:multiple]
  (:select, select_options_html, options)
end

#submit_tag(caption = "Submit", options = {}) ⇒ String

Constructs a submit button from the given options

Examples:

submit_tag "Create", :class => 'success'

Parameters:

  • caption (String) (defaults to: "Submit")

    The caption for the submit button.

  • options (Hash) (defaults to: {})

    The html options for the input field.

Returns:

  • (String)

    The html submit button based on the options specified.



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(caption="Submit", options={})
  options.reverse_merge!(:value => caption)
  input_tag(:submit, options)
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">

Examples:

telephone_field_tag :phone_number, :class => 'string'
# => <input name="phone_number" class="string" type="tel">

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified options



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, options={})
  input_tag(:tel, options.reverse_merge(:name => name))
end

#text_area_tag(name, options = {}) ⇒ String

Constructs a text area input from the given options

Examples:

text_area_tag :username, :class => 'long', :value => "Demo?"

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  options.reverse_merge!(:name => name, :rows => "", :cols => "")
  (:textarea, options.delete(:value).to_s, options)
end

#text_field_tag(name, options = {}) ⇒ String

Creates a text field input with the given name and options

Examples:

text_field_tag :first_name, :maxlength => 40, :required => true
# => <input name="first_name" maxlength="40" required type="text">

text_field_tag :last_name, :class => 'string', :size => 40
# => <input name="last_name" class="string" size="40" type="text">

text_field_tag :username, :placeholder => 'Your Username'
# => <input name="username" placeholder="Your Username" type="text">

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  input_tag(:text, options.reverse_merge!(:name => name))
end

#url_field_tag(name, options = {}) ⇒ String

Creates a url field input with the given name and options

Examples:

url_field_tag :favorite_website, :placeholder => 'http://padrinorb.com'
<input name="favorite_website" placeholder="http://padrinorb.com." type="url">

url_field_tag :home_page, :class => 'string url'
<input name="home_page" class="string url", type="url">

Parameters:

  • name (Symbol)

    The name of the input to create.

  • options (Hash) (defaults to: {})

    The HTML options to include in this field.

Options Hash (options):

  • :id (String)

    Specifies a unique identifier for the field.

  • :class (String)

    Specifies the stylesheet class of the field.

  • :name (String)

    Specifies the name of the field.

  • :accesskey (String)

    Specifies a shortcut key to access the field.

  • :tabindex (Integer)

    Specifies the tab order of the field.

  • :maxlength (Integer)

    Specifies the maximum length, in characters, of the field.

  • :size (Integer)

    Specifies the width, in characters, of the field.

  • :placeholder (String)

    Specifies a short hint that describes the expected value of the field.

  • :hidden (Boolean)

    Specifies whether or not the field is hidden from view.

  • :spellcheck (Boolean)

    Specifies whether or not the field should have it’s spelling and grammar checked for errors.

  • :draggable (Boolean)

    Specifies whether or not the field is draggable. (true, false, :auto)

  • :pattern (String)

    Specifies the regular expression pattern that the field’s value is checked against.

  • :autocomplete (Symbol)

    Specifies whether or not the field should have autocomplete enabled. (:on, :off)

  • :autofocus (Boolean)

    Specifies whether or not the field should automatically get focus when the page loads.

  • :required (Boolean)

    Specifies whether or not the field is required to be completeled before the form is submitted.

  • :readonly (Boolean)

    Specifies whether or not the field is read only.

  • :disabled (Boolean)

    Specifies whether or not the field is disabled.

Returns:

  • (String)

    Generated HTML with specified 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, options={})
  input_tag(:url, options.reverse_merge(:name => name))
end