form_tag_helper.rb 6.8 KB
Newer Older
1 2 3 4 5 6 7
require 'cgi'
require File.dirname(__FILE__) + '/tag_helper'

module ActionView
  module Helpers
    # Provides a number of methods for creating form tags that doesn't rely on conventions with an object assigned to the template like
    # FormHelper does. With the FormTagHelper, you provide the names and values yourself.
8
    #
D
David Heinemeier Hansson 已提交
9
    # NOTE: The html options disabled, readonly, and multiple can all be treated as booleans. So specifying <tt>:disabled => true</tt>
10
    # will give <tt>disabled="disabled"</tt>.
11
    module FormTagHelper
12
      # Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like
13 14 15 16
      # ActionController::Base#url_for. The method for the form defaults to POST.
      #
      # Options:
      # * <tt>:multipart</tt> - If set to true, the enctype is set to "multipart/form-data".
17 18 19
      # * <tt>:method</tt>    - The method to use when submitting the form, usually either "get" or "post".
      #                         If "put", "delete", or another verb is used, a hidden input with name _method 
      #                         is added to simulate the verb over post.
20
      def form_tag(url_for_options = {}, options = {}, *parameters_for_url, &proc)
21
        html_options = options.stringify_keys
22
        html_options["enctype"] = "multipart/form-data" if html_options.delete("multipart")
23 24
        html_options["action"]  = url_for(url_for_options, *parameters_for_url)

25 26
        method_tag = ""
        
27
        case method = html_options.delete("method").to_s
28 29
          when /^get$/i # must be case-insentive, but can't use downcase as might be nil
            html_options["method"] = "get"
30
          when /^post$/i, "", nil
31 32 33 34 35 36 37
            html_options["method"] = "post"
          else
            html_options["method"] = "post"
            method_tag = tag(:input, :type => "hidden", :name => "_method", :value => method)
        end
        
        tag(:form, html_options, true) + method_tag
38 39 40 41 42 43 44 45 46
      end

      alias_method :start_form_tag, :form_tag

      # Outputs "</form>"
      def end_form_tag
        "</form>"
      end

47 48 49 50 51 52 53 54 55 56 57 58
      # Creates a dropdown selection box, or if the <tt>:multiple</tt> option is set to true, a multiple
      # choice selection box.
      #
      # Helpers::FormOptions can be used to create common select boxes such as countries, time zones, or
      # associated records.
      #
      # <tt>option_tags</tt> is a string containing the option tags for the select box:
      #   # Outputs <select id="people" name="people"><option>David</option></select>
      #   select_tag "people", "<option>David</option>"
      #
      # Options:
      # * <tt>:multiple</tt> - If set to true the selection will allow multiple choices.
59
      def select_tag(name, option_tags = nil, options = {})
60
        content_tag :select, option_tags, { "name" => name, "id" => name }.update(options.stringify_keys)
61 62
      end

63 64 65 66 67 68 69 70
      # Creates a standard text field.
      #
      # Options:
      # * <tt>:disabled</tt> - If set to true, the user will not be able to use this input.
      # * <tt>:size</tt> - The number of visible characters that will fit in the input.
      # * <tt>:maxlength</tt> - The maximum number of characters that the browser will allow the user to enter.
      # 
      # A hash of standard HTML options for the tag.
71
      def text_field_tag(name, value = nil, options = {})
72
        tag :input, { "type" => "text", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
73 74
      end

75 76 77
      # Creates a hidden field.
      #
      # Takes the same options as text_field_tag
78
      def hidden_field_tag(name, value = nil, options = {})
79
        text_field_tag(name, value, options.stringify_keys.update("type" => "hidden"))
80 81
      end

82 83 84 85 86 87 88 89 90 91
      # Creates a file upload field.
      #
      # If you are using file uploads then you will also need to set the multipart option for the form:
      #   <%= form_tag { :action => "post" }, { :multipart => true } %>
      #     <label for="file">File to Upload</label> <%= file_field_tag "file" %>
      #     <%= submit_tag %>
      #   <%= end_form_tag %>
      #
      # The specified URL will then be passed a File object containing the selected file, or if the field 
      # was left blank, a StringIO object.
92
      def file_field_tag(name, options = {})
93
        text_field_tag(name, nil, options.update("type" => "file"))
94 95
      end

96 97 98
      # Creates a password field.
      #
      # Takes the same options as text_field_tag
99
      def password_field_tag(name = "password", value = nil, options = {})
100
        text_field_tag(name, value, options.update("type" => "password"))
101 102
      end

103 104 105 106 107
      # Creates a text input area.
      #
      # Options:
      # * <tt>:size</tt> - A string specifying the dimensions of the textarea.
      #     # Outputs <textarea name="body" id="body" cols="25" rows="10"></textarea>
108
      #     <%= text_area_tag "body", nil, :size => "25x10" %>
109
      def text_area_tag(name, content = nil, options = {})
110 111 112 113
        options.stringify_keys!

        if size = options.delete("size")
          options["cols"], options["rows"] = size.split("x")
114
        end
115

116
        content_tag :textarea, content, { "name" => name, "id" => name }.update(options.stringify_keys)
117 118
      end

119
      # Creates a check box.
120
      def check_box_tag(name, value = "1", checked = false, options = {})
121
        html_options = { "type" => "checkbox", "name" => name, "id" => name, "value" => value }.update(options.stringify_keys)
122
        html_options["checked"] = "checked" if checked
123
        tag :input, html_options
124 125
      end

126
      # Creates a radio button.
127
      def radio_button_tag(name, value, checked = false, options = {})
128 129
        pretty_tag_value = value.to_s.gsub(/\s/, "_").gsub(/(?!-)\W/, "").downcase
        html_options = { "type" => "radio", "name" => name, "id" => "#{name}_#{pretty_tag_value}", "value" => value }.update(options.stringify_keys)
130
        html_options["checked"] = "checked" if checked
131
        tag :input, html_options
132 133
      end

134 135
      # Creates a submit button with the text <tt>value</tt> as the caption. If options contains a pair with the key of "disable_with",
      # then the value will be used to rename a disabled version of the submit button.
136
      def submit_tag(value = "Save changes", options = {})
137 138 139 140 141 142 143
        options.stringify_keys!
        
        if disable_with = options.delete("disable_with")
          options["onclick"] = "this.disabled=true;this.value='#{disable_with}';this.form.submit();#{options["onclick"]}"
        end
          
        tag :input, { "type" => "submit", "name" => "commit", "value" => value }.update(options.stringify_keys)
144
      end
145
      
146 147 148
      # Displays an image which when clicked will submit the form.
      #
      # <tt>source</tt> is passed to AssetTagHelper#image_path
149
      def image_submit_tag(source, options = {})
150
        tag :input, { "type" => "image", "src" => image_path(source) }.update(options.stringify_keys)
151
      end
152 153 154
    end
  end
end