Generic FormFor

Generic FormFor is a Rails FormBuilder with easy customization in mind DSL is inspired (read stolen) from Formtastic with some minor changes

Compatibility

Currently tested with rails 3.2.x

Yet another form builder

Mainly all form builders does the same thing. Usually the difference is only in DSL they provide and the outputed HTML. I prefer Formtastic DSL because of its candy DSL. What I do not like is the HTML they generate. Of course, there is always the possibility to fix it all with CSS, but I wanted to make this step easier.

My goal is to create a generic formbuilderi which could easy and quickly adoptable to fit in all my Rails applications. For example, one project is using Bootstrap, from Twitter style, another jQuery UI, another something else. In all these projects I would gladly use one DSL, one form builder – GenericFormFor

Instalation

Simply add GenericFormFor to your Gemfile:


  gem 'generic_form_for'

Run the installation generator:


  $ rails generate generic_form_for:install 

This will generate initializer, form template and i18n yml file

Run the form builder generator:


  $ rails generate generic_form_for:form_builder project1

This will generate new form builder and helper

Open then app/form_builders/project1_form_builder.rb and adjust form output settings

Usage

customize your form builder (in app/form_builders)

Simple example:


  module Project1FormBuilder
    class Project1FormBuilder < GenericFormFor::FormBuilder
      
      #customize form tag
      form_wrapper do
        #add default class for form tag
        form_html :class => "project1-form"
      end
      
      #pack your input with legend and error message
      input_wrapper do
        #wrap your input in block with class input-block
        wrap_in :class => "input-block" do 
          #use label
          label_html
          #here comes input tag intself
          input_html
          #show error messages with class input-error-message
          error_html :class => "input-error-message"
        end
      end
    end
  end

Create forms with new form builder


  <%= project1_form_for @sample do |f| %>
    <%= f.builder %>
    <%= f.fieldset :my_form do %>
      <%=f.input :email %>
      <%=f.input :name %>
    <% end %>
    <%= f.actions do %>
      <%= f.action :submit %>
    <% end %>
  <% end %>

More complex example (perfectly fits with Bootstrap, from Twitter css):


  module Project1FormBuilder
    class Project1FormBuilder < GenericFormFor::FormBuilder
      #Override GenericFormFor default config 
      self.html5_browser_validate = true
      self.html5_browser_autofocus = true
      self.default_text_field_size = 40
      self.required_string = "*"
      self.use_translations = true
      
      #Customize your form builder
      
      #customize form tag
      form_wrapper do
        form_html :class => "form-horizontal"
      end
      
      #customize fieldset and legend tags
      fieldset_wrapper do
        fieldset_html do
          legend_html
        end
      end
      
      #customize your input package tag
      input_wrapper do
        wrap_in :class => "control-group" do 
          label_html :class => "control-label"
          wrap_in :class => "controls" do 
            input_html :class => "input-xlarge"
            error_html :tag => :span, :class => "input-errors"
            hint_html :class => "help-block"
          end
        end
      end
      
      #customize actions wrapper tag
      actions_wrapper do
        actions_html :class => "form-actions"
      end
      
      #customize action (button, input or link) tag
      action_wrapper do
        action_html :class => "btn btn-primary" do
          icon_html :class => "icon-white", :tag => :i
        end
      end
      
    end
  end

Documentation

The Available Inputs

Options for all inputs

  • :wrapper_html => html options for first level wrapper tag if used one
  • :as => what type of input should it be
  • :label_html => html options for label tag
  • :label => label value for input. If symbol given then form_builder will try to localize it from activerecord.attributes.#object_name.#value and form_for.#object_name.attributes.#value. Set to false to hide lable tag at all for input
  • :error_html => html options for error tag
  • :hint_html => html options for hint tag
  • :hint => Hint for input. If symbol given then form_builder will try to localize it from form_for.#object_name.hints.#value
  • :autofocus => set this field as autofocus (works in combination with setting autofocus=>false for form)
  • :required => true|false
  • :class => html class for input

Available inputs

  • boolean
    • :checked => true|false
    • :value => custom value
    • all options that check_box accepts
  • email
    • all options that text_field accepts
    • input tag with type “email”
  • file
    • all options that file_field accepts
  • hidden
    • all options that hidden_field accepts
    • wont print legend, hint and error
  • number
    • all options that text_field accepts
    • input tag with type “number”
  • password
    • all options that password_field accepts
  • phone
    • all options that text_field accepts
    • input tag with type “tel”
  • range
    • all options that text_field accepts
    • input tag with type “range”
  • search
    • all options that text_field accepts
    • input tag with type “search”
  • select
  • string
    • all options that text_field accepts
    • input tag with type “text”
  • text
    • all options that text_area accepts
  • url
    • all options that text_field accepts
    • input tag with type “url”

TODO

  • add configuration option for each input type
  • add checkboxes input
  • add radio input
  • add date/time/datetime input
  • add country input

Contributing to generic_form_for

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Create a pull request on Github

Copyright

Copyright © 2012 Aivars Akots, released under the MIT license.