/labeled_form

Easier creation of default forms in rails

Primary LanguageRubyMIT LicenseMIT

LabeledForm

Automatic adds labels to each of your input fields and wraps them both in an paragraph. Options added to each field helper are:

  • :label - Defines the label text and attributes, can be the following:

    • String - Only defines the label text.

    • false - Produces no label.

    • Hash - Defines the attributes of the label with the following special attributes:

      • :text - Defines the label text.

      • :position - Defines the label position, can be :before or :after. Defaults to :after if the input is a checkbox or a radio button, else :before.

    • The label text defaults to human_attribute_name if model is set, else the humanized key.

  • :wrap - Defines the wrap tag, false results in no wrap. Defaults to :p.

Also includes an fieldset_for method, which extends fields_for with a fieldset tag and legend. Besides default html options, accepts:

  • :legend - Defines the legend, if false produces no legend. Defaults to the human association name or humanized key.

  • :builder - Defines the builder to use. Defaults to the LabeledFormBuilder. This options also applies to fields_for.

Included also is a quick fix for InstanceTag’s to_label_tag method. This method produces the for attribute with the id attribute’s value when the id attribute is given. Fixed this with one little line added. A ticket has been sent to fix this, but in the mean while added the fix in this plugin.

Example

labeled_form_for object do |form| form.text_field :name form.text_area :body, :label => “Content” form.check_box :read form.check_box :write, :label => {:position => :before} form.fieldset_for(:author, :class => “test”) do |fs| fs.text_field :username, :wrap => false end form.submit “Please save” end

Produces: <form …> <p> <label title=“Name” for=“object_name”>Name</label> <input type=“text” value=“My Name” name=“object” id=“object_name” /> </p> <p> <label title=“Content” for=“object_body”>Content</label> <textarea name=“object” id=“object_body”></textarea> </p> <p> <input type=“checkbox” name=“object” id=“object_read” /> <label title=“Read” for=“object_read”>Read</label> </p> <p> <label title=“Write” for=“object_write”>Write</label> <input type=“checkbox” name=“object” id=“object_write” /> </p> <fieldset class=“test”> <legend>Author</legend> <label title=“Username” for=“object_author_username”>Username</label> <input type=“text” value=“user” name=“object[username]” id=“object_author_username” /> </fieldset> <p> <input type=“submit” value=“Please save” name=“commit”> </p> </form>

Copyright © 2009 DiMarcello, released under the MIT license