Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 685 lines (635 sloc) 32.871 kB
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
1 require 'cgi'
0eacdcf @jeremy Use a consistent load path to avoid double requires. Fix some scatter…
jeremy authored
2 require 'action_view/helpers/tag_helper'
76f024a @fxn adds missing requires for Object#blank? and Object#present?
fxn authored
3 require 'active_support/core_ext/object/blank'
330327e @spastorino Call html_escape in ERB::Util module and don't mix it in in the helpers
spastorino authored
4 require 'active_support/core_ext/string/output_safety'
d646d9d @drogus Added config.action_view.embed_authenticity_token_in_remote_forms
drogus authored
5 require 'active_support/core_ext/module/attribute_accessors'
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
6
7 module ActionView
5a0bc2c @rizwanreza Adds title and description where needed.
rizwanreza authored
8 # = Action View Form Tag Helpers
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
9 module Helpers
98dc582 @lifo Merge docrails.
lifo authored
10 # Provides a number of methods for creating form tags that doesn't rely on an Active Record object assigned to the template like
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
11 # FormHelper does. Instead, you provide the names and values manually.
ebf6637 @dhh Added that the html options disabled, readonly, and multiple can all …
dhh authored
12 #
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
13 # NOTE: The HTML options <tt>disabled</tt>, <tt>readonly</tt>, and <tt>multiple</tt> can all be treated as booleans. So specifying
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
14 # <tt>:disabled => true</tt> will give <tt>disabled="disabled"</tt>.
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
15 module FormTagHelper
7b62278 @wycats Make form helpers work with <%=
wycats authored
16 extend ActiveSupport::Concern
17
18 include UrlHelper
19 include TextHelper
20
d646d9d @drogus Added config.action_view.embed_authenticity_token_in_remote_forms
drogus authored
21 mattr_accessor :embed_authenticity_token_in_remote_forms
22 self.embed_authenticity_token_in_remote_forms = true
23
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored
24 # Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
25 # ActionController::Base#url_for. The method for the form defaults to POST.
26 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
27 # ==== Options
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
28 # * <tt>:multipart</tt> - If set to true, the enctype is set to "multipart/form-data".
98dc582 @lifo Merge docrails.
lifo authored
29 # * <tt>:method</tt> - The method to use when submitting the form, usually either "get" or "post".
30 # If "put", "delete", or another verb is used, a hidden input with name <tt>_method</tt>
31 # is added to simulate the verb over post.
5106ce8 authenticity_token option for form_tag [#2988 state:resolved]
Jakub Kuźma authored
32 # * <tt>:authenticity_token</tt> - Authenticity token to use in the form. Use only if you need to
33 # pass custom authenticity token string, or to not add authenticity_token field at all
d646d9d @drogus Added config.action_view.embed_authenticity_token_in_remote_forms
drogus authored
34 # (by passing <tt>false</tt>). Remote forms may omit the embedded authenticity token
35 # by setting <tt>config.action_view.embed_authenticity_token_in_remote_forms = false</tt>.
36 # This is helpful when you're fragment-caching the form. Remote forms get the
37 # authenticity from the <tt>meta</tt> tag, so embedding is unnecessary unless you
38 # support browsers without JavaScript.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
39 # * A list of parameters to feed to the URL the form will be posted to.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
40 # * <tt>:remote</tt> - If set to true, will allow the Unobtrusive JavaScript drivers to control the
ee5cfdf @oem Missed a few instances of British English spelling in the last commit
oem authored
41 # submit behavior. By default this behavior is an ajax submit.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
42 #
43 # ==== Examples
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
44 # form_tag('/posts')
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
45 # # => <form action="/posts" method="post">
46 #
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
47 # form_tag('/posts/1', :method => :put)
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
48 # # => <form action="/posts/1" method="put">
49 #
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
50 # form_tag('/upload', :multipart => true)
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
51 # # => <form action="/upload" method="post" enctype="multipart/form-data">
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
52 #
1662aa1 @amatsuda Added a space before "do" keyword
amatsuda authored
53 # <%= form_tag('/posts') do -%>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
54 # <div><%= submit_tag 'Save' %></div>
55 # <% end -%>
56 # # => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
57 #
8be9d92 @fuzzyalej Fixed documentation error in code examples from form_tag_helper.rb
fuzzyalej authored
58 # <%= form_tag('/posts', :remote => true) %>
c493370 @stefanpenner UJS documentation.
stefanpenner authored
59 # # => <form action="/posts" method="post" data-remote="true">
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
60 #
5106ce8 authenticity_token option for form_tag [#2988 state:resolved]
Jakub Kuźma authored
61 # form_tag('http://far.away.com/form', :authenticity_token => false)
62 # # form without authenticity token
63 #
64 # form_tag('http://far.away.com/form', :authenticity_token => "cf50faa3fe97702ca1ae")
65 # # form with custom authenticity token
66 #
9e81bfd Remove 'parameters_for_url' parameter from 'form_tag' method signature
Gonzalo Rodriguez and Leonardo Capillera authored
67 def form_tag(url_for_options = {}, options = {}, &block)
68 html_options = html_options_for_form(url_for_options, options)
e407b44 @dhh Made FormTagHelper#form_tag work with blocks, rendering start/end_for…
dhh authored
69 if block_given?
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
70 form_tag_in_block(html_options, &block)
e407b44 @dhh Made FormTagHelper#form_tag work with blocks, rendering start/end_for…
dhh authored
71 else
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
72 form_tag_html(html_options)
e407b44 @dhh Made FormTagHelper#form_tag work with blocks, rendering start/end_for…
dhh authored
73 end
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
74 end
75
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
76 # Creates a dropdown selection box, or if the <tt>:multiple</tt> option is set to true, a multiple
77 # choice selection box.
78 #
79 # Helpers::FormOptions can be used to create common select boxes such as countries, time zones, or
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
80 # associated records. <tt>option_tags</tt> is a string containing the option tags for the select box.
81 #
82 # ==== Options
83 # * <tt>:multiple</tt> - If set to true the selection will allow multiple choices.
84 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
c5d54be @danielvlopes fix select_tag to have the same behavior of select
danielvlopes authored
85 # * <tt>:include_blank</tt> - If set to true, an empty option will be create
86 # * <tt>:prompt</tt> - Create a prompt option with blank value and the text asking user to select something
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
87 # * Any other key creates standard HTML attributes for the tag.
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
88 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
89 # ==== Examples
6fc3750 @asanghi eternal confusion! fixed doco to inform correctly
asanghi authored
90 # select_tag "people", options_from_collection_for_select(@people, "id", "name")
dba196c @lifo Merge docrails
lifo authored
91 # # <select id="people" name="people"><option value="1">David</option></select>
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
92 #
c7d8661 @jsnrth Fixed documentation of select tag; options with html need to pass thr…
jsnrth authored
93 # select_tag "people", "<option>David</option>".html_safe
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
94 # # => <select id="people" name="people"><option>David</option></select>
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
95 #
c7d8661 @jsnrth Fixed documentation of select tag; options with html need to pass thr…
jsnrth authored
96 # select_tag "count", "<option>1</option><option>2</option><option>3</option><option>4</option>".html_safe
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
97 # # => <select id="count" name="count"><option>1</option><option>2</option>
98 # # <option>3</option><option>4</option></select>
99 #
c7d8661 @jsnrth Fixed documentation of select tag; options with html need to pass thr…
jsnrth authored
100 # select_tag "colors", "<option>Red</option><option>Green</option><option>Blue</option>".html_safe, :multiple => true
dce6ade @akaspick Ensure select_tag#name attribute uses [] when :multiple is true. [#11…
akaspick authored
101 # # => <select id="colors" multiple="multiple" name="colors[]"><option>Red</option>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
102 # # <option>Green</option><option>Blue</option></select>
103 #
c7d8661 @jsnrth Fixed documentation of select tag; options with html need to pass thr…
jsnrth authored
104 # select_tag "locations", "<option>Home</option><option selected="selected">Work</option><option>Out</option>".html_safe
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
105 # # => <select id="locations" name="locations"><option>Home</option><option selected='selected'>Work</option>
106 # # <option>Out</option></select>
107 #
c7d8661 @jsnrth Fixed documentation of select tag; options with html need to pass thr…
jsnrth authored
108 # select_tag "access", "<option>Read</option><option>Write</option>".html_safe, :multiple => true, :class => 'form_input'
dce6ade @akaspick Ensure select_tag#name attribute uses [] when :multiple is true. [#11…
akaspick authored
109 # # => <select class="form_input" id="access" multiple="multiple" name="access[]"><option>Read</option>
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
110 # # <option>Write</option></select>
111 #
c5d54be @danielvlopes fix select_tag to have the same behavior of select
danielvlopes authored
112 # select_tag "people", options_from_collection_for_select(@people, "id", "name"), :include_blank => true
113 # # => <select id="people" name="people"><option value=""></option><option value="1">David</option></select>
114 #
115 # select_tag "people", options_from_collection_for_select(@people, "id", "name"), :prompt => "Select something"
116 # # => <select id="people" name="people"><option value="">Select something</option><option value="1">David</option></select>
117 #
c7d8661 @jsnrth Fixed documentation of select tag; options with html need to pass thr…
jsnrth authored
118 # select_tag "destination", "<option>NYC</option><option>Paris</option><option>Rome</option>".html_safe, :disabled => true
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
119 # # => <select disabled="disabled" id="destination" name="destination"><option>NYC</option>
120 # # <option>Paris</option><option>Rome</option></select>
ad1fe7d @dhh Completed FormTagHelper by adding radio_button_tag and select_tag
dhh authored
121 def select_tag(name, option_tags = nil, options = {})
dce6ade @akaspick Ensure select_tag#name attribute uses [] when :multiple is true. [#11…
akaspick authored
122 html_name = (options[:multiple] == true && !name.to_s.ends_with?("[]")) ? "#{name}[]" : name
c5d54be @danielvlopes fix select_tag to have the same behavior of select
danielvlopes authored
123
124 if options.delete(:include_blank)
6d0526d @spastorino escape select_tag :prompt values
spastorino authored
125 option_tags = content_tag(:option, '', :value => '').safe_concat(option_tags)
1191e3f @rizwanreza Add :include_blank option for select_tag [#1987 status:resolved]
rizwanreza authored
126 end
c5d54be @danielvlopes fix select_tag to have the same behavior of select
danielvlopes authored
127
128 if prompt = options.delete(:prompt)
6d0526d @spastorino escape select_tag :prompt values
spastorino authored
129 option_tags = content_tag(:option, prompt, :value => '').safe_concat(option_tags)
c5d54be @danielvlopes fix select_tag to have the same behavior of select
danielvlopes authored
130 end
131
5fad229 @geekq Fixed that FormTagHelper generates illegal html if name contains e.g.…
geekq authored
132 content_tag :select, option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
ad1fe7d @dhh Completed FormTagHelper by adding radio_button_tag and select_tag
dhh authored
133 end
134
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
135 # Creates a standard text field; use these text fields to input smaller chunks of text like a username
136 # or a search query.
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
137 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
138 # ==== Options
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
139 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
140 # * <tt>:size</tt> - The number of visible characters that will fit in the input.
141 # * <tt>:maxlength</tt> - The maximum number of characters that the browser will allow the user to enter.
73dafba @remear Added :placeholder option to ActionView::Helpers::FormTagHelper text_…
remear authored
142 # * <tt>:placeholder</tt> - The text contained in the field by default which is removed when the field receives focus.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
143 # * Any other key creates standard HTML attributes for the tag.
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
144 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
145 # ==== Examples
146 # text_field_tag 'name'
147 # # => <input id="name" name="name" type="text" />
148 #
149 # text_field_tag 'query', 'Enter your search query here'
150 # # => <input id="query" name="query" type="text" value="Enter your search query here" />
151 #
73dafba @remear Added :placeholder option to ActionView::Helpers::FormTagHelper text_…
remear authored
152 # text_field_tag 'search', nil, :placeholder => 'Enter search term...'
153 # # => <input id="search" name="search" placeholder="Enter search term..." type="text" />
154 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
155 # text_field_tag 'request', nil, :class => 'special_input'
156 # # => <input class="special_input" id="request" name="request" type="text" />
157 #
158 # text_field_tag 'address', '', :size => 75
159 # # => <input id="address" name="address" size="75" type="text" value="" />
160 #
161 # text_field_tag 'zip', nil, :maxlength => 5
162 # # => <input id="zip" maxlength="5" name="zip" type="text" />
163 #
164 # text_field_tag 'payment_amount', '$0.00', :disabled => true
165 # # => <input disabled="disabled" id="payment_amount" name="payment_amount" type="text" value="$0.00" />
166 #
167 # text_field_tag 'ip', '0.0.0.0', :maxlength => 15, :size => 20, :class => "ip-input"
168 # # => <input class="ip-input" id="ip" maxlength="15" name="ip" size="20" type="text" value="0.0.0.0" />
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
169 def text_field_tag(name, value = nil, options = {})
5fad229 @geekq Fixed that FormTagHelper generates illegal html if name contains e.g.…
geekq authored
170 tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
7701e64 @NZKoz Add label_tag helper for generating elements. Closes #10802 [DefV]
NZKoz authored
171 end
172
6e69b42 @stephencelis Let label helpers accept blocks.
stephencelis authored
173 # Creates a label element. Accepts a block.
7701e64 @NZKoz Add label_tag helper for generating elements. Closes #10802 [DefV]
NZKoz authored
174 #
392817c @erikstmartin updating link_to and button_to to support :remote => true and other o…
erikstmartin authored
175 # ==== Options
7701e64 @NZKoz Add label_tag helper for generating elements. Closes #10802 [DefV]
NZKoz authored
176 # * Creates standard HTML attributes for the tag.
177 #
178 # ==== Examples
179 # label_tag 'name'
180 # # => <label for="name">Name</label>
181 #
182 # label_tag 'name', 'Your name'
183 # # => <label for="name">Your Name</label>
184 #
185 # label_tag 'name', nil, :class => 'small_label'
186 # # => <label for="name" class="small_label">Name</label>
d18a274 @josevalim Improve previous patch a bit [#3645 state:resolved]
josevalim authored
187 def label_tag(name = nil, content_or_options = nil, options = nil, &block)
eb69068 @spastorino Tiny refactor
spastorino authored
188 if block_given? && content_or_options.is_a?(Hash)
189 options = content_or_options = content_or_options.stringify_keys
190 else
191 options ||= {}
192 options = options.stringify_keys
193 end
6e69b42 @stephencelis Let label helpers accept blocks.
stephencelis authored
194 options["for"] = sanitize_to_id(name) unless name.blank? || options.has_key?("for")
eb69068 @spastorino Tiny refactor
spastorino authored
195 content_tag :label, content_or_options || name.to_s.humanize, options, &block
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
196 end
197
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
198 # Creates a hidden form input field used to transmit data that would be lost due to HTTP's statelessness or
199 # data that should be hidden from the user.
200 #
201 # ==== Options
202 # * Creates standard HTML attributes for the tag.
203 #
204 # ==== Examples
205 # hidden_field_tag 'tags_list'
206 # # => <input id="tags_list" name="tags_list" type="hidden" />
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
207 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
208 # hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
209 # # => <input id="token" name="token" type="hidden" value="VUBJKB23UIVI1UU1VOBVI@" />
210 #
211 # hidden_field_tag 'collected_input', '', :onchange => "alert('Input collected!')"
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
212 # # => <input id="collected_input" name="collected_input" onchange="alert('Input collected!')"
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
213 # # type="hidden" value="" />
ad1fe7d @dhh Completed FormTagHelper by adding radio_button_tag and select_tag
dhh authored
214 def hidden_field_tag(name, value = nil, options = {})
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored
215 text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
ad1fe7d @dhh Completed FormTagHelper by adding radio_button_tag and select_tag
dhh authored
216 end
217
fcdb5dc @smartinez87 Remove extra white spaces on ActionPack docs.
smartinez87 authored
218 # Creates a file upload field. If you are using file uploads then you will also need
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
219 # to set the multipart option for the form tag:
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
220 #
9ed919e @fxn revises some <%= in rdoc
fxn authored
221 # <%= form_tag '/upload', :multipart => true do %>
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
222 # <label for="file">File to Upload</label> <%= file_field_tag "file" %>
223 # <%= submit_tag %>
6ef3546 @lifo Merge docrails
lifo authored
224 # <% end %>
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
225 #
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
226 # The specified URL will then be passed a File object containing the selected file, or if the field
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
227 # was left blank, a StringIO object.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
228 #
229 # ==== Options
230 # * Creates standard HTML attributes for the tag.
231 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
232 #
233 # ==== Examples
234 # file_field_tag 'attachment'
235 # # => <input id="attachment" name="attachment" type="file" />
236 #
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
237 # file_field_tag 'avatar', :class => 'profile_input'
238 # # => <input class="profile_input" id="avatar" name="avatar" type="file" />
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
239 #
240 # file_field_tag 'picture', :disabled => true
241 # # => <input disabled="disabled" id="picture" name="picture" type="file" />
242 #
243 # file_field_tag 'resume', :value => '~/resume.doc'
244 # # => <input id="resume" name="resume" type="file" value="~/resume.doc" />
245 #
246 # file_field_tag 'user_pic', :accept => 'image/png,image/gif,image/jpeg'
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
247 # # => <input accept="image/png,image/gif,image/jpeg" id="user_pic" name="user_pic" type="file" />
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
248 #
249 # file_field_tag 'file', :accept => 'text/html', :class => 'upload', :value => 'index.html'
250 # # => <input accept="text/html" class="upload" id="file" name="file" type="file" value="index.html" />
df7f345 @dhh Added FormHelper#file_field and FormTagHelper#file_field_tag for crea…
dhh authored
251 def file_field_tag(name, options = {})
064842f Convert boolean form options form the tag_helper. Recloses #809.
Marcel Molina authored
252 text_field_tag(name, nil, options.update("type" => "file"))
df7f345 @dhh Added FormHelper#file_field and FormTagHelper#file_field_tag for crea…
dhh authored
253 end
254
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
255 # Creates a password field, a masked text field that will hide the users input behind a mask character.
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
256 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
257 # ==== Options
258 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
259 # * <tt>:size</tt> - The number of visible characters that will fit in the input.
260 # * <tt>:maxlength</tt> - The maximum number of characters that the browser will allow the user to enter.
261 # * Any other key creates standard HTML attributes for the tag.
262 #
263 # ==== Examples
264 # password_field_tag 'pass'
265 # # => <input id="pass" name="pass" type="password" />
266 #
267 # password_field_tag 'secret', 'Your secret here'
268 # # => <input id="secret" name="secret" type="password" value="Your secret here" />
269 #
270 # password_field_tag 'masked', nil, :class => 'masked_input_field'
271 # # => <input class="masked_input_field" id="masked" name="masked" type="password" />
272 #
273 # password_field_tag 'token', '', :size => 15
274 # # => <input id="token" name="token" size="15" type="password" value="" />
275 #
276 # password_field_tag 'key', nil, :maxlength => 16
277 # # => <input id="key" maxlength="16" name="key" type="password" />
278 #
279 # password_field_tag 'confirm_pass', nil, :disabled => true
280 # # => <input disabled="disabled" id="confirm_pass" name="confirm_pass" type="password" />
281 #
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
282 # password_field_tag 'pin', '1234', :maxlength => 4, :size => 6, :class => "pin_input"
283 # # => <input class="pin_input" id="pin" maxlength="4" name="pin" size="6" type="password" value="1234" />
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
284 def password_field_tag(name = "password", value = nil, options = {})
064842f Convert boolean form options form the tag_helper. Recloses #809.
Marcel Molina authored
285 text_field_tag(name, value, options.update("type" => "password"))
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
286 end
287
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
288 # Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
289 #
290 # ==== Options
0230fc9 Disambiguate :size option for text area tag. Closes #8955 [redbeard]
Marcel Molina authored
291 # * <tt>:size</tt> - A string specifying the dimensions (columns by rows) of the textarea (e.g., "25x10").
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
292 # * <tt>:rows</tt> - Specify the number of rows in the textarea
293 # * <tt>:cols</tt> - Specify the number of columns in the textarea
294 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
085db5e @chrismear Make text_area_tag escape contents by default.
chrismear authored
295 # * <tt>:escape</tt> - By default, the contents of the text input are HTML escaped.
296 # If you need unescaped contents, set this to false.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
297 # * Any other key creates standard HTML attributes for the tag.
298 #
299 # ==== Examples
300 # text_area_tag 'post'
301 # # => <textarea id="post" name="post"></textarea>
302 #
303 # text_area_tag 'bio', @user.bio
304 # # => <textarea id="bio" name="bio">This is my biography.</textarea>
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
305 #
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
306 # text_area_tag 'body', nil, :rows => 10, :cols => 25
307 # # => <textarea cols="25" id="body" name="body" rows="10"></textarea>
308 #
309 # text_area_tag 'body', nil, :size => "25x10"
310 # # => <textarea name="body" id="body" cols="25" rows="10"></textarea>
311 #
312 # text_area_tag 'description', "Description goes here.", :disabled => true
313 # # => <textarea disabled="disabled" id="description" name="description">Description goes here.</textarea>
314 #
315 # text_area_tag 'comment', nil, :class => 'comment_input'
316 # # => <textarea class="comment_input" id="comment" name="comment"></textarea>
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
317 def text_area_tag(name, content = nil, options = {})
54b8356 @waynn fix stringify_keys destructive behavior for most FormTagHelper functions
waynn authored
318 options = options.stringify_keys
00541f2 @dhh Added :disable_with option to FormTagHelper#submit_tag to allow for e…
dhh authored
319
320 if size = options.delete("size")
1dc56ba @dhh Fixed that FormTagHelper#text_area_tag should disregard :size option …
dhh authored
321 options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
322 end
dfac1ce @dhh Fixed that form helpers would treat string and symbol keys differentl…
dhh authored
323
085db5e @chrismear Make text_area_tag escape contents by default.
chrismear authored
324 escape = options.key?("escape") ? options.delete("escape") : true
330327e @spastorino Call html_escape in ERB::Util module and don't mix it in in the helpers
spastorino authored
325 content = ERB::Util.html_escape(content) if escape
085db5e @chrismear Make text_area_tag escape contents by default.
chrismear authored
326
d8d38be @jeremy HTML safety: fix textarea with nil content
jeremy authored
327 content_tag :textarea, content.to_s.html_safe, { "name" => name, "id" => sanitize_to_id(name) }.update(options)
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
328 end
329
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
330 # Creates a check box form input tag.
331 #
332 # ==== Options
333 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
334 # * Any other key creates standard HTML options for the tag.
335 #
336 # ==== Examples
337 # check_box_tag 'accept'
338 # # => <input id="accept" name="accept" type="checkbox" value="1" />
339 #
340 # check_box_tag 'rock', 'rock music'
341 # # => <input id="rock" name="rock" type="checkbox" value="rock music" />
342 #
343 # check_box_tag 'receive_email', 'yes', true
344 # # => <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />
345 #
346 # check_box_tag 'tos', 'yes', false, :class => 'accept_tos'
347 # # => <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />
348 #
349 # check_box_tag 'eula', 'accepted', false, :disabled => true
350 # # => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
351 def check_box_tag(name, value = "1", checked = false, options = {})
5fad229 @geekq Fixed that FormTagHelper generates illegal html if name contains e.g.…
geekq authored
352 html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
353 html_options["checked"] = "checked" if checked
00541f2 @dhh Added :disable_with option to FormTagHelper#submit_tag to allow for e…
dhh authored
354 tag :input, html_options
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
355 end
356
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
357 # Creates a radio button; use groups of radio buttons named the same to allow users to
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
358 # select from a group of options.
359 #
360 # ==== Options
361 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
362 # * Any other key creates standard HTML options for the tag.
363 #
364 # ==== Examples
365 # radio_button_tag 'gender', 'male'
366 # # => <input id="gender_male" name="gender" type="radio" value="male" />
367 #
368 # radio_button_tag 'receive_updates', 'no', true
369 # # => <input checked="checked" id="receive_updates_no" name="receive_updates" type="radio" value="no" />
370 #
371 # radio_button_tag 'time_slot', "3:00 p.m.", false, :disabled => true
372 # # => <input disabled="disabled" id="time_slot_300_pm" name="time_slot" type="radio" value="3:00 p.m." />
373 #
374 # radio_button_tag 'color', "green", true, :class => "color_input"
375 # # => <input checked="checked" class="color_input" id="color_green" name="color" type="radio" value="green" />
ad1fe7d @dhh Completed FormTagHelper by adding radio_button_tag and select_tag
dhh authored
376 def radio_button_tag(name, value, checked = false, options = {})
c01014a @sikachu Modify the behavior of `radio_button_tag` to use `sanitize_to_id` for…
sikachu authored
377 html_options = { "type" => "radio", "name" => name, "id" => "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}", "value" => value }.update(options.stringify_keys)
ad1fe7d @dhh Completed FormTagHelper by adding radio_button_tag and select_tag
dhh authored
378 html_options["checked"] = "checked" if checked
00541f2 @dhh Added :disable_with option to FormTagHelper#submit_tag to allow for e…
dhh authored
379 tag :input, html_options
ad1fe7d @dhh Completed FormTagHelper by adding radio_button_tag and select_tag
dhh authored
380 end
381
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
382 # Creates a submit button with the text <tt>value</tt> as the caption.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
383 #
384 # ==== Options
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
385 # * <tt>:confirm => 'question?'</tt> - If present the unobtrusive JavaScript
386 # drivers will provide a prompt with the question specified. If the user accepts,
c493370 @stefanpenner UJS documentation.
stefanpenner authored
387 # the form is processed normally, otherwise no action is taken.
dc4eec1 @lifo Merge docrails:
lifo authored
388 # * <tt>:disabled</tt> - If true, the user will not be able to use this input.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
389 # * <tt>:disable_with</tt> - Value of this parameter will be used as the value for a
390 # disabled version of the submit button when the form is submitted. This feature is
c493370 @stefanpenner UJS documentation.
stefanpenner authored
391 # provided by the unobtrusive JavaScript driver.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
392 # * Any other key creates standard HTML options for the tag.
393 #
394 # ==== Examples
395 # submit_tag
396 # # => <input name="commit" type="submit" value="Save changes" />
397 #
398 # submit_tag "Edit this article"
399 # # => <input name="commit" type="submit" value="Edit this article" />
400 #
401 # submit_tag "Save edits", :disabled => true
402 # # => <input disabled="disabled" name="commit" type="submit" value="Save edits" />
403 #
404 # submit_tag "Complete sale", :disable_with => "Please wait..."
63c6fee @vijaydev revise some doc changes
vijaydev authored
405 # # => <input name="commit" data-disable-with="Please wait..." type="submit" value="Complete sale" />
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
406 #
407 # submit_tag nil, :class => "form_submit"
408 # # => <input class="form_submit" name="commit" type="submit" />
409 #
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
410 # submit_tag "Edit", :disable_with => "Editing...", :class => "edit_button"
63c6fee @vijaydev revise some doc changes
vijaydev authored
411 # # => <input class="edit_button" data-disable_with="Editing..." name="commit" type="submit" value="Edit" />
c493370 @stefanpenner UJS documentation.
stefanpenner authored
412 #
413 # submit_tag "Save", :confirm => "Are you sure?"
63c6fee @vijaydev revise some doc changes
vijaydev authored
414 # # => <input name='commit' type='submit' value='Save' data-confirm="Are you sure?" />
c493370 @stefanpenner UJS documentation.
stefanpenner authored
415 #
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
416 def submit_tag(value = "Save changes", options = {})
54b8356 @waynn fix stringify_keys destructive behavior for most FormTagHelper functions
waynn authored
417 options = options.stringify_keys
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
418
00541f2 @dhh Added :disable_with option to FormTagHelper#submit_tag to allow for e…
dhh authored
419 if disable_with = options.delete("disable_with")
e7f0d37 @jeremy Remove superfluous condition
jeremy authored
420 options["data-disable-with"] = disable_with
00541f2 @dhh Added :disable_with option to FormTagHelper#submit_tag to allow for e…
dhh authored
421 end
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
422
123f5a3 @dhh Added :confirm option to submit_tag (closes #11415) [miloops]
dhh authored
423 if confirm = options.delete("confirm")
0c824e3 @spastorino Refactor a bit this code to add data-confirm and data-disable-with
spastorino authored
424 options["data-confirm"] = confirm
123f5a3 @dhh Added :confirm option to submit_tag (closes #11415) [miloops]
dhh authored
425 end
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
426
1f270e8 @waynn remove redundant calls to stringify_keys
waynn authored
427 tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options)
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
428 end
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
429
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
430 # Creates a button element that defines a <tt>submit</tt> button,
431 # <tt>reset</tt>button or a generic button which can be used in
432 # JavaScript, for example. You can use the button tag as a regular
433 # submit tag but it isn't supported in legacy browsers. However,
829de9d @tomstuart Add block support to button_tag helper
tomstuart authored
434 # the button tag allows richer labels such as images and emphasis,
435 # so this helper will also accept a block.
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
436 #
437 # ==== Options
438 # * <tt>:confirm => 'question?'</tt> - If present, the
439 # unobtrusive JavaScript drivers will provide a prompt with
440 # the question specified. If the user accepts, the form is
441 # processed normally, otherwise no action is taken.
442 # * <tt>:disabled</tt> - If true, the user will not be able to
443 # use this input.
444 # * <tt>:disable_with</tt> - Value of this parameter will be
445 # used as the value for a disabled version of the submit
446 # button when the form is submitted. This feature is provided
447 # by the unobtrusive JavaScript driver.
448 # * Any other key creates standard HTML options for the tag.
449 #
450 # ==== Examples
451 # button_tag
03749d6 @tomstuart Make type="submit" the default for button_tag helper
tomstuart authored
452 # # => <button name="button" type="submit">Button</button>
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
453 #
829de9d @tomstuart Add block support to button_tag helper
tomstuart authored
454 # button_tag(:type => 'button') do
455 # content_tag(:strong, 'Ask me!')
456 # end
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
457 # # => <button name="button" type="button">
5f57d2a @fuzzyalej Fixed formatting of code examples in form_tag_helper.rb
fuzzyalej authored
458 # # <strong>Ask me!</strong>
459 # # </button>
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
460 #
461 # button_tag "Checkout", :disable_with => "Please wait..."
63c6fee @vijaydev revise some doc changes
vijaydev authored
462 # # => <button data-disable-with="Please wait..." name="button" type="submit">Checkout</button>
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
463 #
829de9d @tomstuart Add block support to button_tag helper
tomstuart authored
464 def button_tag(content_or_options = nil, options = nil, &block)
465 options = content_or_options if block_given? && content_or_options.is_a?(Hash)
466 options ||= {}
54b8356 @waynn fix stringify_keys destructive behavior for most FormTagHelper functions
waynn authored
467 options = options.stringify_keys
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
468
469 if disable_with = options.delete("disable_with")
470 options["data-disable-with"] = disable_with
471 end
472
473 if confirm = options.delete("confirm")
474 options["data-confirm"] = confirm
475 end
476
03749d6 @tomstuart Make type="submit" the default for button_tag helper
tomstuart authored
477 options.reverse_merge! 'name' => 'button', 'type' => 'submit'
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
478
829de9d @tomstuart Add block support to button_tag helper
tomstuart authored
479 content_tag :button, content_or_options || 'Button', options, &block
18605ad @rizwanreza HTML5 button_tag helper
rizwanreza authored
480 end
481
ecb0e39 @dhh Added additional documentation to FormTagHelper #1788 [jon@instance-d…
dhh authored
482 # Displays an image which when clicked will submit the form.
483 #
8b2266a @fxn image_path -> path_to_image in a couple of places, plus motivation fo…
fxn authored
484 # <tt>source</tt> is passed to AssetTagHelper#path_to_image
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
485 #
486 # ==== Options
b141624 @pyrat Added image_submit_tag confirm option [status:committed #784]
pyrat authored
487 # * <tt>:confirm => 'question?'</tt> - This will add a JavaScript confirm
488 # prompt with the question specified. If the user accepts, the form is
489 # processed normally, otherwise no action is taken.
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
490 # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
491 # * Any other key creates standard HTML options for the tag.
492 #
493 # ==== Examples
494 # image_submit_tag("login.png")
495 # # => <input src="/images/login.png" type="image" />
496 #
b18585f @NZKoz Fix syntax errors in the documentation for image submit tag. Closes …
NZKoz authored
497 # image_submit_tag("purchase.png", :disabled => true)
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
498 # # => <input disabled="disabled" src="/images/purchase.png" type="image" />
499 #
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
500 # image_submit_tag("search.png", :class => 'search_button')
501 # # => <input class="search_button" src="/images/search.png" type="image" />
b00e6a9 @dhh Massive documentation update for all helpers (closes #8223, #8177, #8…
dhh authored
502 #
5f808b8 @dhh Consistently use lowercase instead of camelCase for all JS class name…
dhh authored
503 # image_submit_tag("agree.png", :disabled => true, :class => "agree_disagree_button")
504 # # => <input class="agree_disagree_button" disabled="disabled" src="/images/agree.png" type="image" />
d2fb072 @dhh Added FormTagHelper#image_submit_tag for making submit buttons that u…
dhh authored
505 def image_submit_tag(source, options = {})
54b8356 @waynn fix stringify_keys destructive behavior for most FormTagHelper functions
waynn authored
506 options = options.stringify_keys
b141624 @pyrat Added image_submit_tag confirm option [status:committed #784]
pyrat authored
507
508 if confirm = options.delete("confirm")
0c824e3 @spastorino Refactor a bit this code to add data-confirm and data-disable-with
spastorino authored
509 options["data-confirm"] = confirm
b141624 @pyrat Added image_submit_tag confirm option [status:committed #784]
pyrat authored
510 end
511
1f270e8 @waynn remove redundant calls to stringify_keys
waynn authored
512 tag :input, { "type" => "image", "src" => path_to_image(source) }.update(options)
d2fb072 @dhh Added FormTagHelper#image_submit_tag for making submit buttons that u…
dhh authored
513 end
0e6c8e5 @NZKoz Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]
NZKoz authored
514
515 # Creates a field set for grouping HTML form elements.
516 #
517 # <tt>legend</tt> will become the fieldset's title (optional as per W3C).
8c105ee @akaspick Add options to field_set_tag
akaspick authored
518 # <tt>options</tt> accept the same values as tag.
0e6c8e5 @NZKoz Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]
NZKoz authored
519 #
5b92dcb Merge docrails
lifo authored
520 # ==== Examples
67de0af @jeroenvandijk Updated documentation for block helpers in form_tag_helper.rb
jeroenvandijk authored
521 # <%= field_set_tag do %>
0e6c8e5 @NZKoz Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]
NZKoz authored
522 # <p><%= text_field_tag 'name' %></p>
523 # <% end %>
524 # # => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>
525 #
67de0af @jeroenvandijk Updated documentation for block helpers in form_tag_helper.rb
jeroenvandijk authored
526 # <%= field_set_tag 'Your details' do %>
0e6c8e5 @NZKoz Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]
NZKoz authored
527 # <p><%= text_field_tag 'name' %></p>
528 # <% end %>
529 # # => <fieldset><legend>Your details</legend><p><input id="name" name="name" type="text" /></p></fieldset>
8c105ee @akaspick Add options to field_set_tag
akaspick authored
530 #
67de0af @jeroenvandijk Updated documentation for block helpers in form_tag_helper.rb
jeroenvandijk authored
531 # <%= field_set_tag nil, :class => 'format' do %>
8c105ee @akaspick Add options to field_set_tag
akaspick authored
532 # <p><%= text_field_tag 'name' %></p>
533 # <% end %>
534 # # => <fieldset class="format"><p><input id="name" name="name" type="text" /></p></fieldset>
535 def field_set_tag(legend = nil, options = nil, &block)
0e6c8e5 @NZKoz Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]
NZKoz authored
536 content = capture(&block)
7b62278 @wycats Make form helpers work with <%=
wycats authored
537 output = tag(:fieldset, options, true)
538 output.safe_concat(content_tag(:legend, legend)) unless legend.blank?
539 output.concat(content)
540 output.safe_concat("</fieldset>")
0e6c8e5 @NZKoz Add fieldset_tag for generating fieldsets, closes #9477. [djanowski]
NZKoz authored
541 end
f8730e5 @dhh Added all the new HTML5 form types as individual form tag methods (se…
dhh authored
542
543 # Creates a text field of type "search".
544 #
545 # ==== Options
546 # * Accepts the same options as text_field_tag.
547 def search_field_tag(name, value = nil, options = {})
548 text_field_tag(name, value, options.stringify_keys.update("type" => "search"))
549 end
550
551 # Creates a text field of type "tel".
552 #
553 # ==== Options
554 # * Accepts the same options as text_field_tag.
555 def telephone_field_tag(name, value = nil, options = {})
556 text_field_tag(name, value, options.stringify_keys.update("type" => "tel"))
557 end
558 alias phone_field_tag telephone_field_tag
559
560 # Creates a text field of type "url".
561 #
562 # ==== Options
563 # * Accepts the same options as text_field_tag.
564 def url_field_tag(name, value = nil, options = {})
565 text_field_tag(name, value, options.stringify_keys.update("type" => "url"))
566 end
567
568 # Creates a text field of type "email".
569 #
570 # ==== Options
571 # * Accepts the same options as text_field_tag.
572 def email_field_tag(name, value = nil, options = {})
573 text_field_tag(name, value, options.stringify_keys.update("type" => "email"))
574 end
575
576 # Creates a number field.
577 #
578 # ==== Options
579 # * <tt>:min</tt> - The minimum acceptable value.
580 # * <tt>:max</tt> - The maximum acceptable value.
581 # * <tt>:in</tt> - A range specifying the <tt>:min</tt> and
582 # <tt>:max</tt> values.
583 # * <tt>:step</tt> - The acceptable value granularity.
584 # * Otherwise accepts the same options as text_field_tag.
585 #
586 # ==== Examples
587 # number_field_tag 'quantity', nil, :in => 1...10
8be9d92 @fuzzyalej Fixed documentation error in code examples from form_tag_helper.rb
fuzzyalej authored
588 # # => <input id="quantity" name="quantity" min="1" max="9" type="number" />
f8730e5 @dhh Added all the new HTML5 form types as individual form tag methods (se…
dhh authored
589 def number_field_tag(name, value = nil, options = {})
590 options = options.stringify_keys
591 options["type"] ||= "number"
592 if range = options.delete("in") || options.delete("within")
593 options.update("min" => range.min, "max" => range.max)
594 end
595 text_field_tag(name, value, options)
596 end
597
598 # Creates a range form element.
599 #
600 # ==== Options
601 # * Accepts the same options as number_field_tag.
602 def range_field_tag(name, value = nil, options = {})
603 number_field_tag(name, value, options.stringify_keys.update("type" => "range"))
604 end
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
605
91e3046 @dlee Make utf8_enforcer_tag an overrideable method
dlee authored
606 # Creates the hidden UTF8 enforcer tag. Override this method in a helper
e294009 @josevalim No need for a configuration option here.
josevalim authored
607 # to customize the tag.
91e3046 @dlee Make utf8_enforcer_tag an overrideable method
dlee authored
608 def utf8_enforcer_tag
e294009 @josevalim No need for a configuration option here.
josevalim authored
609 tag(:input, :type => "hidden", :name => "utf8", :value => "&#x2713;".html_safe)
91e3046 @dlee Make utf8_enforcer_tag an overrideable method
dlee authored
610 end
611
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
612 private
9e81bfd Remove 'parameters_for_url' parameter from 'form_tag' method signature
Gonzalo Rodriguez and Leonardo Capillera authored
613 def html_options_for_form(url_for_options, options)
b1cfced @spastorino Change returning with tap
spastorino authored
614 options.stringify_keys.tap do |html_options|
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
615 html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart")
cba1460 @fxn url_for no longer escapes HTML, the :escape option is also gone
fxn authored
616 # The following URL is unescaped, this is just a hash of options, and it is the
87e9e3f @rtlechow Action Pack typos.
rtlechow authored
617 # responsibility of the caller to escape all the values.
9e81bfd Remove 'parameters_for_url' parameter from 'form_tag' method signature
Gonzalo Rodriguez and Leonardo Capillera authored
618 html_options["action"] = url_for(url_for_options)
6fe424a @wycats Small typo
wycats authored
619 html_options["accept-charset"] = "UTF-8"
d646d9d @drogus Added config.action_view.embed_authenticity_token_in_remote_forms
drogus authored
620
e50abba @dhh Allow you to force the authenticity_token to be rendered even on remo…
dhh authored
621 html_options["data-remote"] = true if html_options.delete("remote")
16ee611 @dhh Do not include the authenticity token in forms where remote: true as …
dhh authored
622
d646d9d @drogus Added config.action_view.embed_authenticity_token_in_remote_forms
drogus authored
623 if html_options["data-remote"] &&
624 !embed_authenticity_token_in_remote_forms &&
d5980b5 @drogus Cover one more case in auth_token and remote forms
drogus authored
625 html_options["authenticity_token"].blank?
d646d9d @drogus Added config.action_view.embed_authenticity_token_in_remote_forms
drogus authored
626 # The authenticity token is taken from the meta tag in this case
627 html_options["authenticity_token"] = false
628 elsif html_options["authenticity_token"] == true
e50abba @dhh Allow you to force the authenticity_token to be rendered even on remo…
dhh authored
629 # Include the default authenticity_token, which is only generated when its set to nil,
630 # but we needed the true value to override the default of no authenticity_token on data-remote.
631 html_options["authenticity_token"] = nil
16ee611 @dhh Do not include the authenticity token in forms where remote: true as …
dhh authored
632 end
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
633 end
634 end
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
635
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
636 def extra_tags_for_form(html_options)
5106ce8 authenticity_token option for form_tag [#2988 state:resolved]
Jakub Kuźma authored
637 authenticity_token = html_options.delete("authenticity_token")
25215d7 @wycats Fix several known web encoding issues:
wycats authored
638 method = html_options.delete("method").to_s
639
640 method_tag = case method
ccf9577 @dolzenko Fix a bunch of minor spelling mistakes
dolzenko authored
641 when /^get$/i # must be case-insensitive, but can't use downcase as might be nil
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
642 html_options["method"] = "get"
643 ''
644 when /^post$/i, "", nil
645 html_options["method"] = "post"
5106ce8 authenticity_token option for form_tag [#2988 state:resolved]
Jakub Kuźma authored
646 token_tag(authenticity_token)
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
647 else
648 html_options["method"] = "post"
5106ce8 authenticity_token option for form_tag [#2988 state:resolved]
Jakub Kuźma authored
649 tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag(authenticity_token)
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
650 end
25215d7 @wycats Fix several known web encoding issues:
wycats authored
651
fa3fc52 @dlee Make utf8 enforcer param customizeable
dlee authored
652 tags = utf8_enforcer_tag << method_tag
25215d7 @wycats Fix several known web encoding issues:
wycats authored
653 content_tag(:div, tags, :style => 'margin:0;padding:0;display:inline')
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
654 end
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
655
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
656 def form_tag_html(html_options)
657 extra_tags = extra_tags_for_form(html_options)
4cbb9db For performance reasons, you can no longer call html_safe! on Strings…
Yehuda Katz authored
658 (tag(:form, html_options, true) + extra_tags).html_safe
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
659 end
72f93b5 @jeremy Check whether blocks are called from erb using a special __in_erb_tem…
jeremy authored
660
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
661 def form_tag_in_block(html_options, &block)
662 content = capture(&block)
7b62278 @wycats Make form helpers work with <%=
wycats authored
663 output = ActiveSupport::SafeBuffer.new
664 output.safe_concat(form_tag_html(html_options))
665 output << content
666 output.safe_concat("</form>")
1ff8450 @technoweenie Refactor #form_tag to allow easy extending. [Rick]
technoweenie authored
667 end
4e3ed5b @technoweenie Merge csrf_killer plugin into rails. Adds RequestForgeryProtection m…
technoweenie authored
668
5106ce8 authenticity_token option for form_tag [#2988 state:resolved]
Jakub Kuźma authored
669 def token_tag(token)
670 if token == false || !protect_against_forgery?
4e3ed5b @technoweenie Merge csrf_killer plugin into rails. Adds RequestForgeryProtection m…
technoweenie authored
671 ''
672 else
9b08afd if ... nil? is more expensive than unless
Milan Dobrota authored
673 token ||= form_authenticity_token
5106ce8 authenticity_token option for form_tag [#2988 state:resolved]
Jakub Kuźma authored
674 tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => token)
4e3ed5b @technoweenie Merge csrf_killer plugin into rails. Adds RequestForgeryProtection m…
technoweenie authored
675 end
676 end
5fad229 @geekq Fixed that FormTagHelper generates illegal html if name contains e.g.…
geekq authored
677
678 # see http://www.w3.org/TR/html4/types.html#type-name
679 def sanitize_to_id(name)
680 name.to_s.gsub(']','').gsub(/[^-a-zA-Z0-9:.]/, "_")
681 end
967339e @dhh Added FormTagHelper that provides a number of methods for creating fo…
dhh authored
682 end
683 end
684 end
Something went wrong with that request. Please try again.