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

DATETIME_ATTRIBUTES =
[:value, :max, :min].freeze
COLOR_CODE_REGEXP =
/\A#([0-9a-fA-F]{3}){1,2}\z/.freeze

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.



508
509
510
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 508

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

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(caption, url, options = {}) ⇒ String

    Parameters:

    • caption (String)

      The text caption.

    • url (String)

      The url href.

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

      The html options.

  • #button_to(url, 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.



581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 581

def button_to(*args, &block)
  options   = args.extract_options!.dup
  name, url = *args
  options['data-remote'] = 'true' if options.delete(:remote)
  submit_options = options.delete(:submit_options) || {}
  form_tag(url || name, options) do
    if block_given?
      (:button, capture_html(&block), submit_options)
    else
      submit_tag(name, submit_options)
    end
  end
end

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

Constructs a check_box from the given options.

Examples:

check_box_tag :remember_me, :value => 'Yes'


421
422
423
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 421

def check_box_tag(name, options={})
  input_tag(:checkbox, { :name => name, :value => '1' }.update(options))
end

#color_field_tag(name, options = {}) ⇒ Object

Constructs a color tag from the given options.

Examples:

color_field_tag('color', :value => "#ff0000")
color_field_tag('color', :value => "#f00")

Parameters:

  • name (String)

    The name of the color field.

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

    The html options for the color field.

Options Hash (options):

  • :value (String)

    The value of the color field. See examples for details.



795
796
797
798
799
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 795

def color_field_tag(name, options = {})
  options = { :name => name }.update(options)
  options[:value] = adjust_color(options[:value])
  input_tag(:color, options)
end

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

Constructs a date tag from the given options.

Examples:

date_field_tag('date_with_min_max', :min => DateTime.new(1993, 2, 24),
                                    :max => DateTime.new(2000, 4, 1))
date_field_tag('date_with_value', :value => DateTime.new(2000, 4, 1))

Parameters:

  • name (String)

    The name of the date field.

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

    The html options for the date field.

Options Hash (options):

  • :min (DateTime, String)

    The min date time of the date field.

  • :max (DateTime, String)

    The max date time of the date field.

  • :value (DateTime, String)

    The value of the date field. See examples for details.

Returns:

  • (String)

    The html date field



697
698
699
700
701
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 697

def date_field_tag(name, options = {})
  options = { :name => name }.update(options)
  options = convert_attributes_into_datetime("%Y-%m-%d", options)
  input_tag(:date, options)
end

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

Constructs a datetime tag from the given options.

Examples:

datetime_field_tag('datetime_with_min_max', :min => DateTime.new(1993, 2, 24, 12, 30, 45),
                                            :max => DateTime.new(2000, 4, 1, 12, 0, 0))
datetime_field_tag('datetime_with_value', :value => DateTime.new(2000, 4, 1, 12, 0, 0))

Parameters:

  • name (String)

    The name of the datetime field.

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

    The html options for the datetime field.

Options Hash (options):

  • :min (DateTime, String)

    The min date time of the datetime field.

  • :max (DateTime, String)

    The max date time of the datetime field.

  • :value (DateTime, String)

    The value of the datetime field. See examples for details.

Returns:

  • (String)

    The html datetime field



645
646
647
648
649
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 645

def datetime_field_tag(name, options = {})
  options = { :name => name }.update(options)
  options = convert_attributes_into_datetime("%Y-%m-%dT%T.%L%z", options)
  input_tag(:datetime, options)
end

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

Constructs a datetime-local tag from the given options.

Examples:

datetime_local_field_tag('datetime_local_with_min_max', :min => DateTime.new(1993, 2, 24, 12, 30, 45),
                                                        :max => DateTime.new(2000, 4, 1, 12, 0, 0))
datetime_local_field_tag('datetime_local_with_value', :value => DateTime.new(2000, 4, 1, 12, 0, 0))

Parameters:

  • name (String)

    The name of the datetime local field.

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

    The html options for the datetime-local field.

Options Hash (options):

  • :min (DateTime, String)

    The min date time of the datetime-local field.

  • :max (DateTime, String)

    The max date time of the datetime-local field.

  • :value (DateTime, String)

    The value of the datetime field. See examples for details.

Returns:

  • (String)

    The html datetime-local field



671
672
673
674
675
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 671

def datetime_local_field_tag(name, options = {})
  options = { :name => name }.update(options)
  options = convert_attributes_into_datetime("%Y-%m-%dT%T", options)
  input_tag(:datetime-local", options)
end

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

Creates an email field input with the given name and options.

Examples:

email_field_tag :email, :placeholder => 'you@example.com'
# => <input name="email" placeholder="you@example.com" type="email" />

email_field_tag :email, :value => 'padrinorb@gmail.com', :readonly => true
# => <input name="email" value="padrinorb@gmail.com" 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 completed 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.



341
342
343
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 341

def email_field_tag(name, options={})
  input_tag(:email, { :name => name }.update(options))
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.



145
146
147
148
149
150
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 145

def field_set_tag(*args, &block)
  options = args.extract_options!
  legend_text = args.first
  legend_html = legend_text.blank? ? ActiveSupport::SafeBuffer.new : (:legend, legend_text)
  concat_content (:fieldset, legend_html << capture_html(&block), options)
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.

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.

  • options (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.



69
70
71
72
73
74
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 69

def fields_for(object, options={}, &block)
  instance = builder_instance(object, options)
  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.

Examples:

file_field_tag :photo, :class => 'long'


442
443
444
445
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 442

def file_field_tag(name, options={})
  name = "#{name}[]" if options[:multiple]
  input_tag(:file, { :name => name }.update(options))
end

#form_for(object, url, options = {}, &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
form_for @user, '/register', :as => :customer do |f| ... end

Parameters:

  • object (Object)

    The object for which the form is being built.

  • URL (String)

    The url this form will submit to.

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

    The settings associated with this form. Accepts a :namespace option that will be prepended to the id attributes of the form's elements. Also accepts HTML options.

  • block (Proc)

    The fields and content inside this form.

  • settings (Hash)

    a customizable set of options

Yields:

  • (AbstractFormBuilder)

    The form builder used to compose fields.

Returns:

  • (String)

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



44
45
46
47
48
49
50
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 44

def form_for(object, url, options={}, &block)
  instance = builder_instance(object, options)
  # this can erect instance.multipart flag if the block calls instance.file_field
  html = capture_html(instance, &block)
  options = { :multipart => instance.multipart }.update(options.reject{ |key,_| [:namespace, :as].include?(key) })
  form_tag(url, options) { 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.



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, options={}, &block)
  options = {
    :action => url,
    :protect_from_csrf => is_protected_from_csrf?,
    'accept-charset' => 'UTF-8'
  }.update(options)
  options[:enctype] = 'multipart/form-data' if options.delete(:multipart)

  if (desired_method = options[:method]) =~ /get/i
    options.delete(:protect_from_csrf)
  else
    options[:method] = 'post'
  end
  inner_form_html = hidden_form_method_field(desired_method)
  inner_form_html << csrf_token_field if options.delete(:protect_from_csrf)
  concat_content (:form, inner_form_html << capture_html(&block), options)
end

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

Constructs a hidden field input from the given options.

Examples:

hidden_field_tag :session_key, :value => "__secret__"


389
390
391
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 389

def hidden_field_tag(name, options={})
  input_tag(:hidden, { :name => name }.update(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.



123
124
125
126
# File 'padrino-helpers/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.

Examples:

image_submit_tag 'form/submit.png'

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.



546
547
548
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 546

def image_submit_tag(source, options={})
  input_tag(:image, { :src => image_path(source) }.update(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.



170
171
172
173
174
175
176
177
178
179
180
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 170

def label_tag(name, options={}, &block)
  options = { :caption => "#{name.to_s.humanize}: ", :for => name }.update(options)
  caption_text = ActiveSupport::SafeBuffer.new << options.delete(:caption)
  caption_text << "<span class='required'>*</span> ".html_safe if options.delete(:required)

  if block_given?
    concat_content (:label, caption_text << capture_html(&block), options)
  else
    (:label, caption_text, options)
  end
end

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

Constructs a month tag from the given options.

Examples:

month_field_tag('month_with_min_max', :min => DateTime.new(1993, 2, 24),
                                      :max => DateTime.new(2000, 4, 1))
month_field_tag('month_with_value', :value => DateTime.new(2000, 4, 1))

Parameters:

  • name (String)

    The name of the month field.

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

    The html options for the month field.

Options Hash (options):

  • :min (DateTime, String)

    The min month time of the month field.

  • :max (DateTime, String)

    The max month time of the month field.

  • :value (DateTime, String)

    The value of the month field. See examples for details.

Returns:

  • (String)

    The html month field



723
724
725
726
727
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 723

def month_field_tag(name, options = {})
  options = { :name => name }.update(options)
  options = convert_attributes_into_datetime("%Y-%m", options)
  input_tag(:month, options)
end

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

Creates a number field input with the given name and options.

Examples:

number_field_tag :quantity, :class => 'numeric'
# => <input name="quantity" 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.



303
304
305
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 303

def number_field_tag(name, options={})
  input_tag(:number, { :name => name }.update(options))
end

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

Constructs a password field input from the given options.

Examples:

password_field_tag :password, :class => 'long'


411
412
413
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 411

def password_field_tag(name, options={})
  input_tag(:password, { :name => name }.update(options))
end

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

Constructs a radio_button from the given options.

Examples:

radio_button_tag :remember_me, :value => 'true'


431
432
433
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 431

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

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

Constructs a range tag from the given options.

Examples:

range_field_tag('ranger_with_min_max', :min => 1, :max => 50)
range_field_tag('ranger_with_range', :range => 1..5)

Parameters:

  • name (String)

    The name of the range field.

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

    The html options for the range field.

Options Hash (options):

  • :min (Integer)

    The min range of the range field.

  • :max (Integer)

    The max range of the range field.

  • :range (range)

    The range, in lieu of :min and :max. See examples for details.

Returns:

  • (String)

    The html range field



614
615
616
617
618
619
620
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 614

def range_field_tag(name, options = {})
  options = { :name => name }.update(options)
  if range = options.delete(:range)
    options[:min], options[:max] = range.min, range.max
  end
  input_tag(:range, 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 completed 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.



363
364
365
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 363

def search_field_tag(name, options={})
  input_tag(:search, { :name => name }.update(options))
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 the rails-style attribute hash.
grouped_options = {
  "Friends" => ["Yoda", ["Obiwan", 2, {:magister => 'no'}], {:lame => 'yes'}],
  "Enemies" => [["Palpatine", "Palpatine", {:scary => 'yes', :old => 'yes'}], ["Darth Vader", 3, {:disabled => true}]]
}
select_tag(:name, :grouped_options => grouped_options)

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.



489
490
491
492
493
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 489

def select_tag(name, options={})
  options = { :name => name }.merge(options)
  options[:name] = "#{options[:name]}[]" if options[:multiple]
  (:select, extract_option_tags!(options), options)
end

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

Constructs a submit button from the given options.

Examples:

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

Overloads:

  • #submit_tag(options = {}) ⇒ String

    Parameters:

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

      The html options for the input field.

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

    Parameters:

    • caption (String)

      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.



527
528
529
530
531
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 527

def submit_tag(*args)
  options = args.extract_options!
  caption = args.length >= 1 ? args.first : "Submit"
  input_tag(:submit, { :value => caption }.merge(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 completed 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.



324
325
326
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 324

def telephone_field_tag(name, options={})
  input_tag(:tel, { :name => name }.update(options))
end

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

Constructs a text area input from the given options.

Examples:

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


399
400
401
402
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 399

def text_area_tag(name, options={})
  inner_html = TagHelpers::NEWLINE + options.delete(:value).to_s
  (:textarea, inner_html, { :name => name }.update(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 completed 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.



239
240
241
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 239

def text_field_tag(name, options={})
  input_tag(:text, { :name => name }.update(options))
end

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

Constructs a time tag from the given options.

Examples:

time_field_tag('time_with_min_max', :max => Time.new(1993, 2, 24, 1, 19, 12),
                                    :min => Time.new(2008, 6, 21, 13, 30, 0))
time_field_tag('time_with_value', :value => Time.new(2008, 6, 21, 13, 30, 0))

Parameters:

  • name (String)

    The name of the time field.

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

    The html options for the time field.

Options Hash (options):

  • :min (Time, DateTime, String)

    The min time of the time field.

  • :max (Time, DateTime, String)

    The max time of the time field.

  • :value (Time, DateTime, String)

    The value of the time field. See examples for details.

Returns:

  • (String)

    The html time field



775
776
777
778
779
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 775

def time_field_tag(name, options = {})
  options = { :name => name }.update(options)
  options = convert_attributes_into_datetime("%T.%L", options)
  input_tag(:time, options)
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 completed 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.



379
380
381
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 379

def url_field_tag(name, options={})
  input_tag(:url, { :name => name }.update(options))
end

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

Constructs a week tag from the given options.

Examples:

week_field_tag('week_with_min_max', :min => DateTime.new(1993, 2, 24),
                                    :max => DateTime.new(2000, 4, 1))
week_field_tag('week_with_value', :value => DateTime.new(2000, 4, 1))

Parameters:

  • name (String)

    The name of the week field.

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

    The html options for the week field.

Options Hash (options):

  • :min (DateTime, String)

    The min week time of the week field.

  • :max (DateTime, String)

    The max week time of the week field.

  • :value (DateTime, String)

    The value of the week field. See examples for details.

Returns:

  • (String)

    The html week field



749
750
751
752
753
# File 'padrino-helpers/lib/padrino-helpers/form_helpers.rb', line 749

def week_field_tag(name, options = {})
  options = { :name => name }.update(options)
  options = convert_attributes_into_datetime("%Y-W%W", options)
  input_tag(:week, options)
end