Repository is archived
No commit activity in last 3 years
No release in over 3 years
This gem for Ruby on Rails 4.0+ allows to create blocks for each field of the form. This allows to simplify syntax, show errors, add errors classes and validation attributes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.7
~> 10.0
>= 0

Runtime

~> 4.0
 Project Readme

FormGroups for Rails

Warning: This gem is still in the active development phase. Please read the code before deciding that it is suitable for production.

This gem for Ruby on Rails 4.0+ allows to create blocks for each field of the form. This allows to simplify syntax, show errors, add errors classes and validation attributes.

For example consider this part of the template:

<div class='field <%= 'field-errors' if @report.errors[:name].any? %>'>
  <%= f.label :name, 'Name' %>
  <%= f.text_field :name, placeholder: 'Name' %>

  <% if @report.errors[:name].any? %>
    <p><%= @report.errors[:name].first %></p>
  <% end %>
</div>

With this gem you can write following code instead:

<%= f.field :name do |name| %>
  <%= name.label 'Name' %>
  <%= name.text 'Name' %>
  <% if name.errors.any? %>
    <p><%= name.errors.first %></p>
  <% end %>
<% end %>

If model assosiated with the form has validations, helpers will try to convert them to attributes. By default only presence and length will be converted to jQuery Validation-compatible attributes, but you can define your own or turn off this feature altogether.

You can configure default field and error classes as well as mapping from validators to attributes in the initializer like this:

# config/initializers/form_groups.rb
FormGroups.configure do |config|
  config.field_class = 'field'
  config.field_error_class = 'field-errors'

  config.validator_mapping[ActiveModel::Validations::LengthValidator] = Proc.new do |validator, html|
    html['data-min'] = validator.options[:minimum] if validator.options[:minimum]
    html['data-max'] = validator.options[:maximum] if validator.options[:maximum]
  end

  config.map_validators = false # Add this to turn off validators mapping
end

The validator_mapping option will check if given validator should be mapped with #is_a? method therefore hierarchy of mappings is possible.