Main form class that contains all the required methods to generate form specific tags, such as textareas and select boxes. Do note that this class is not thread-safe so you should modify it only within one thread of execution.
Constructor method that generates an instance of the Form class.
@param [Object] form_values Object containing the values for each form
field.
@param [Hash] options A hash containing any additional form attributes. @return [Object] An instance of the Form class.
# File lib/ramaze/helper/blue_form.rb, line 139 139: def initialize(form_values, options) 140: @form_values = form_values 141: @form_args = options.dup 142: @g = Gestalt.new 143: end
Builds the form by generating the opening/closing tags and executing the methods in the block.
@param [Hash] form_errors Hash containing all form errors (if any).
# File lib/ramaze/helper/blue_form.rb, line 151 151: def build(form_errors = {}) 152: # Convert all the keys in form_errors to strings and 153: # retrieve the correct values in case 154: @form_errors = {} 155: 156: form_errors.each do |key, value| 157: if value.respond_to?(:first) 158: value = value.first 159: end 160: 161: @form_errors[key.to_s] = value 162: end 163: 164: @g.form(@form_args) do 165: if block_given? 166: yield self 167: end 168: end 169: end
Generate a fieldset tag.
@param [Block] &block The form elements to display inside the fieldset. @example
form_for(@data, :method => :post) do |f| f.fieldset do f.legend 'Hello, world!' end end
# File lib/ramaze/helper/blue_form.rb, line 195 195: def fieldset(&block) 196: @g.fieldset(&block) 197: end
Generate an input tag with a type of “checkbox”.
If you want to have multiple checkboxes you can either use an array or a hash. In the case of an array the values will also be used as text for each checkbox. When using a hash the key will be displayed and the value will be the value of the checkbox. Example:
@data = Class.new attr_reader :gender_arr attr_reader :gender_hash def initialize @gender_arr = ['male', 'female'] @gender_hash = {"Male" => "male", "Female" => "female"} end end.new form_for(@data, :method => :post) do |f| f.input_checkbox "Gender", :gender_arr f.input_checkbox "Gender", :gender_hash end
@example
form_for(@data, :method => :post) do |f| f.input_checkbox 'Remember me', :remember_user end
@param [String] label The text to display inside the label tag. @param [String Symbol] name The name of the checkbox. @param [String/Array] checked String or array that indicates which
value(s) should be checked.
@param [Hash] args Any additional HTML attributes along with their
values.
@option args [String/Symbol] :id The value to use for the ID attribute. @option args [Array] :values An array containing the possible values
for the checkboxes.
@option args [String/Symbol] :span_class The class to use for the
<span> element that's wrapped around the checkbox.
@option args [TrueClass/FalseClass] :show_value When set to false the
value of each checkbox won't be displayed to the right of the checkbox. This option is set to true by default.
@option args [TrueClass/FalseClass] :show_label When set to true
(default) the label for the checkbox will be displayed. Setting this to false will hide it.
# File lib/ramaze/helper/blue_form.rb, line 328 328: def input_checkbox(label, name, checked = nil, args = {}) 329: id = args[:id] ? args[:id] : "#{id_for(name)}_0" 330: 331: # Determine whether or not to show the value of the checkbox 332: if args.key?(:show_value) 333: show_value = args.delete(:show_value) 334: else 335: show_value = true 336: end 337: 338: # Determine whether or not to show the label 339: if args.key?(:show_label) 340: show_label = args.delete(:show_label) 341: else 342: show_label = true 343: end 344: 345: # Get the checkbox value from either the args hash or from 346: # the form object (as specified in the form_for() method). 347: if !args[:values] and @form_values.respond_to?(name) 348: args[:values] = @form_values.send(name) 349: end 350: 351: # That class for each element wrapper (a span tag) can be customized 352: # using :span_class => "a_class". 353: if args[:span_class] 354: span_class = args[:span_class] 355: args.delete(:span_class) 356: else 357: span_class = "checkbox_wrap" 358: end 359: 360: # Get the type from the args hash instead of pre-defining it. Doing so 361: # means we can use this method for the input_radio method. 362: args[:type] = :checkbox if !args[:type] 363: 364: # Convert the values to an array if it's something we can't use in a loop 365: # (e.g. a string). 366: if args[:values].class != Hash and args[:values].class != Array 367: args[:values] = [args[:values]] 368: end 369: 370: # Create a checkbox for each value 371: if !args[:values].empty? 372: @g.p do 373: # Let's create the label and the hidden field 374: if show_label === true 375: label_for(id, label, name) 376: end 377: 378: # Loop through all the values. Each checkbox will have an ID of 379: # "form-NAME-INDEX". Each name will be NAME followed by [] to 380: # indicate it's an array (since multiple values are possible). 381: args[:values].each_with_index do |value, index| 382: id = args[:id] ? args[:id] : "#{id_for(name)}_#{index}" 383: 384: if args[:type] == :checkbox 385: checkbox_name = "#{name}[]" 386: else 387: checkbox_name = name 388: end 389: 390: # Copy all additional attributes and their values except the 391: # values array. 392: opts = args.clone 393: opts.delete(:values) 394: 395: # Get the value and text to display for each checkbox 396: if value.class == Array 397: checkbox_text = value[0] 398: checkbox_value = value[1] 399: else 400: checkbox_text = checkbox_value = value 401: end 402: 403: # Let's see if the current item is checked 404: if checked.class == Array 405: if checked.include?(checkbox_value) 406: opts[:checked] = 'checked' 407: end 408: else 409: if checkbox_value == checked 410: opts[:checked] = 'checked' 411: end 412: end 413: 414: # And we're done, easy wasn't it? 415: opts = opts.merge( 416: :name => checkbox_name, :id => id, :value => checkbox_value 417: ) 418: 419: # Generate the following HTML: 420: # 421: # <span class="#{span_class}"> 422: # <input type="checkbox" name="#{checkbox_name}" id="#{id}" 423: # value="#{value}" /> #{value} 424: # </span> 425: # 426: @g.span(:class => span_class) do 427: @g.input(opts) 428: " #{checkbox_text}" if show_value === true 429: end 430: end 431: end 432: end 433: end
Generate a field for uploading files.
@param [String] label The text to display inside the label tag. @param [String Symbol] name The name of the radio tag. @param [Hash] args Any additional HTML attributes along with their
values.
@example
form_for(@data, :method => :post) do |f| f.input_file 'Image', :image end
# File lib/ramaze/helper/blue_form.rb, line 495 495: def input_file(label, name, args = {}) 496: id = args[:id] ? args[:id] : id_for(name) 497: args = args.merge(:type => :file, :name => name, :id => id) 498: 499: @g.p do 500: label_for(id, label, name) 501: @g.input(args) 502: end 503: end
Generate an input tag with a type of “password” along with a label. Password fields are pretty much the same as text fields except that the content of these fields is replaced with dots. This method has the following alias: “password”.
@param [String] label The text to display inside the label tag. @param [String Symbol] name The name of the password field. @param [Hash] args Any additional HTML attributes along with their
values.
@example
form_for(@data, :method => :post) do |f| f.input_password 'My password', :password end
# File lib/ramaze/helper/blue_form.rb, line 244 244: def input_password(label, name, args = {}) 245: # The ID can come from 2 places, id_for and the args hash 246: id = args[:id] ? args[:id] : id_for(name) 247: args = args.merge(:type => :password, :name => name, :id => id) 248: 249: if !args[:value] and @form_values.respond_to?(name) 250: args[:value] = @form_values.send(name) 251: end 252: 253: @g.p do 254: label_for(id, label, name) 255: @g.input(args) 256: end 257: end
Generate an input tag with a type of “radio”.
If you want to generate multiple radio buttons you can use an array just like you can with checkboxes. Example:
@data = Class.new attr_reader :gender_arr attr_reader :gender_hash def initialize @gender_arr = ['male', 'female'] @gender_hash = {"Male" => "male", "Female" => "female"} end end.new form_for(@data, :method => :post) do |f| f.input_radio "Gender", :gender_arr f.input_radio "Gender", :gender_hash end
For more information see the input_checkbox() method.
@param [String] label The text to display inside the label tag. @param [String Symbol] name The name of the radio button. @param [String] checked String that indicates if (and which) radio
button should be checked.
@param [Hash] args Any additional HTML attributes along with their
values.
@see input_checkbox() @example
form_for(@data, :method => :post) do |f| f.input_radio 'Gender', :gender end
# File lib/ramaze/helper/blue_form.rb, line 471 471: def input_radio(label, name, checked = nil, args = {}) 472: # Force a type of "radio" 473: args[:type] = :radio 474: 475: if !args[:span_class] 476: args[:span_class] = "radio_wrap" 477: end 478: 479: self.input_checkbox(label, name, checked, args) 480: end
Generate a submit tag (without a label). A submit tag is a button that once it’s clicked will send the form data to the server.
@param [String] value The text to display in the button. @param [Hash] args Any additional HTML attributes along with their
values.
@example
form_for(@data, :method => :post) do |f| f.input_submit 'Save' end
# File lib/ramaze/helper/blue_form.rb, line 272 272: def input_submit(value = nil, args = {}) 273: args = args.merge(:type => :submit) 274: args[:value] = value unless value.nil? 275: 276: @g.p do 277: @g.input(args) 278: end 279: end
Generate an input tag with a type of “text” along with a label tag. This method also has the alias “text” so feel free to use that one instead of input_text.
@param [String] label The text to display inside the label tag. @param [String Symbol] name The name of the text field. @param [Hash] args Any additional HTML attributes along with their
values.
@example
form_for(@data, :method => :post) do |f| f.input_text 'Username', :username end
# File lib/ramaze/helper/blue_form.rb, line 213 213: def input_text(label, name, args = {}) 214: # The ID can come from 2 places, id_for and the args hash 215: id = args[:id] ? args[:id] : id_for(name) 216: args = args.merge(:type => :text, :name => name, :id => id) 217: 218: if !args[:value] and @form_values.respond_to?(name) 219: args[:value] = @form_values.send(name) 220: end 221: 222: @g.p do 223: label_for(id, label, name) 224: @g.input(args) 225: end 226: end
Generate a
@param [String] text The text to display inside the legend tag. @example
form_for(@data, :method => :post) do |f| f.legend 'Ramaze rocks!' end
# File lib/ramaze/helper/blue_form.rb, line 180 180: def legend(text) 181: @g.legend(text) 182: end
Generate a select tag along with the option tags and a label.
@param [String] label The text to display inside the label tag. @param [String Symbol] name The name of the select tag. @param [Hash] args Hash containing additional HTML attributes. @example
form_for(@data, :method => :post) do |f| f.select 'Country', :country_list end
# File lib/ramaze/helper/blue_form.rb, line 574 574: def select(label, name, args = {}) 575: id = args[:id] ? args[:id] : id_for(name) 576: multiple, size = args.values_at(:multiple, :size) 577: 578: # Get all the values 579: if !args[:values] and @form_values.respond_to?(name) 580: values = @form_values.send(name) 581: else 582: values = args[:values] 583: args.delete(:values) 584: end 585: 586: args[:multiple] = 'multiple' if multiple 587: args[:size] = (size || values.count || 1).to_i 588: args[:name] = multiple ? "#{name}[]" : name 589: args = args.merge(:id => id) 590: 591: # Retrieve the selected value 592: has_selected, selected = args.key?(:selected), args[:selected] 593: selected = [selected] if !selected.is_a?(Array) 594: args.delete(:selected) 595: 596: @g.p do 597: label_for(id, label, name) 598: @g.select args do 599: values.each do |value, o_name| 600: o_name ||= value 601: o_args = {:value => value} 602: 603: if has_selected and selected.include?(value) 604: o_args[:selected] = 'selected' 605: end 606: 607: @g.option(o_args){ o_name } 608: end 609: end 610: end 611: end
Generate a text area.
@param [String] label The text to display inside the label tag. @param [String Symbol] name The name of the textarea. @param [Hash] args Any additional HTML attributes along with their
values.
@example
form_for(@data, :method => :post) do |f| f.textarea 'Description', :description end
# File lib/ramaze/helper/blue_form.rb, line 544 544: def textarea(label, name, args = {}) 545: id = args[:id] ? args[:id] : id_for(name) 546: 547: # Get the value of the textarea 548: if !args[:value] and @form_values.respond_to?(name) 549: value = @form_values.send(name) 550: else 551: value = args[:value] 552: args.delete(:value) 553: end 554: 555: args = args.merge(:name => name, :id => id) 556: 557: @g.p do 558: label_for(id, label, name) 559: @g.textarea(args){ value } 560: end 561: end
Method used for converting the results of the BlueForm helper to a string
@return [String] The form output
# File lib/ramaze/helper/blue_form.rb, line 619 619: def to_s 620: @g.to_s 621: end
Generate a value for an ID tag based on the field’s name.
@param [String] field_name The name of the field. @return [String] The ID for the specified field name.
# File lib/ramaze/helper/blue_form.rb, line 646 646: def id_for(field_name) 647: if name = @form_args[:name] 648: "#{name}_#{field_name}".downcase.gsub(/-/, '_') 649: else 650: "form_#{field_name}".downcase.gsub(/-/, '_') 651: end 652: end
Generate a label based on the id and value.
@param [String] id The ID to which the label belongs. @param [String] value The text to display inside the label tag. @param [String] name The name of the field to which the label belongs.
# File lib/ramaze/helper/blue_form.rb, line 632 632: def label_for(id, value, name) 633: if error = @form_errors.delete(name.to_s) 634: @g.label("#{value} ", :for => id){ @g.span(:class => :error){ error } } 635: else 636: @g.label(value, :for => id) 637: end 638: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.