{"id":2051,"date":"2011-05-18T14:10:22","date_gmt":"2011-05-18T17:10:22","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=2051"},"modified":"2011-05-18T14:18:33","modified_gmt":"2011-05-18T17:18:33","slug":"simpleform-1-4-is-out","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2011\/05\/simpleform-1-4-is-out\/","title":{"rendered":"SimpleForm 1.4 is out"},"content":{"rendered":"

I’m pleased to say that we released SimpleForm 1.4<\/a>. Like the last version, this release had a lot of contributions from the community, closing bugs and adding some nice features. Here is a brief introduction to some of the new features:<\/p>\n

Custom Form Builders<\/h3>\n

Now you can set a custom form builder\u00a0that inherits from SimpleForm::FormBuilder<\/code>:<\/p>\n

class CustomBuilder < SimpleForm::FormBuilder\r\n  def input(attribute_name, options={}, &block)\r\n    options[:input_html].merge! :class => 'custom'\r\n    super\r\n  end\r\nend<\/pre>\n

And use it straight in the simple_form_for<\/code> helper, like the example below:<\/p>\n

<%= simple_form_for(@user, :builder => CustomBuilder) do |f| %>\r\n  <%= f.input :name %>\r\n<% end %><\/pre>\n

Custom Inputs<\/h3>\n

SimpleForm has many different inputs available in its source code. But, sometimes, depending on the business logic the application requires, we need to add new inputs to make our work easier. Before this version, you had to explicitly define your new input inside SimpleForm namespace for it to work. Furthermore, customizing existing SimpleForm inputs could only be achieved through monkey patching.<\/p>\n

Inspired by a similar feature in the Formtastic<\/a> gem, from now on you will be able to create new input types inside app\/inputs<\/code> folder in your application. The only restriction to create such inputs is that the class name must end with Input<\/code>. See some examples:<\/p>\n

# app\/inputs\/currency_input.rb\r\nclass CurrencyInput < SimpleForm::Inputs::StringInput\r\n  def input\r\n    \"$ #{super}\".html_safe\r\n  end\r\nend<\/pre>\n

And the usage:<\/p>\n

f.input :money, :as => :currency<\/pre>\n

You can also redefine existing SimpleForm inputs by creating a new class with the same name. For instance, if you want to wrap date\/time\/datetime inputs in a div, you can do:<\/p>\n

# app\/inputs\/date_time_input.rb\r\nclass DateTimeInput < SimpleForm::Inputs::DateTimeInput\r\n  def input\r\n    \"
#{super}<\/div>\".html_safe\r\n end\r\nend<\/pre>\n

HTML 5<\/h3>\n

SimpleForm allows you to add many HTML 5 features to your applications, like placeholders, inline browser validations and more. The problem is: most browsers are still experimenting some HTML 5 features, and people started having\u00a0lots of troubles with the automatic browser validation.<\/p>\n

For this reason, SimpleForm now has an option to easily disable such form validations. You have to add this line to your SimpleForm initializer:<\/p>\n

config.browser_validations\u00a0=\u00a0false<\/pre>\n

But, if HTML 5 is still not for you, you can disable all the HTML 5 stuff, by adding the configuration below to your initializer:<\/p>\n

config.html5\u00a0=\u00a0false<\/pre>\n

Notice that this option does not disable the `placeholder` component, because we believe this option is very well supported currently in mostly browsers. If you don't want to use it as well, just remove it from the `components` option in your initializer.<\/p>\n

More Helpers<\/h3>\n

In this version we also add two new form helpers to SimpleForm: input_field<\/code> and full_error<\/code>.<\/p>\n

The full_error<\/code> helper shows errors in an attribute prepending its human name. This can be used when you want to show errors on hidden fields, for instance. You can see how it works in this example:<\/p>\n

f.full_error :token #=> Token is invalid<\/span><\/pre>\n

The input_field<\/code> helper renders only the input tag with all the facilities of SimpleForm's input helper. It means no wrapper, error or hint will be rendered. A good example of using this helper is inside an input block:<\/p>\n

<%= f.input :max_time, :as => :integer do %>\r\n  <%= f.input_field :max_time, :as => :integer, :type => :range %>\r\n  <%= content_tag :span, '1', :id => 'max_time_value' %>\r\n<% end %><\/pre>\n

It will render:<\/p>\n

\r\n
\r\n\u00a0