Skip to content
Browse files

added css_class method and test for user specified css class

1 parent c57fdfc commit 1e8bba7eb378da86cabb3d6a0ece9b34998c58e2 @patrick99e99 patrick99e99 committed Jul 25, 2011
19 lib/simple_form/action_view_extensions/form_helper.rb
@@ -31,21 +31,26 @@ def with_custom_field_error_proc(&block)
def simple_form_for(record, options={}, &block)
options[:builder] ||= SimpleForm::FormBuilder
- css_class = case record
- when String, Symbol then record.to_s
- when Array then dom_class(record.last)
- else dom_class(record)
- end
options[:html] ||= {}
unless options[:html].key?(:novalidate)
options[:html][:novalidate] = !SimpleForm.browser_validations
- options[:html][:class] = "#{SimpleForm.form_class} #{css_class} #{options[:html][:class]}".strip
+ options[:html][:class] = [SimpleForm.form_class, css_class(record, options[:html])].compact.join(" ")
with_custom_field_error_proc do
form_for(record, options, &block)
+ def css_class(record, html_options)
+ if html_options.key?(:class)
+ html_options[:class]
+ elsif record.is_a?(String) || record.is_a?(Symbol)
+ record
+ else
+ record = record.last if record.is_a?(Array)
+ dom_class(record)
+ end
+ end
def simple_fields_for(record_name, record_object = nil, options = {}, &block)
options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
10 test/action_view_extensions/form_helper_test.rb
@@ -46,6 +46,16 @@ class FormHelperTest < ActionView::TestCase
concat(simple_form_for(@user) do |f| end)
assert_select 'form.simple_form.user'
+ test 'simple form should not add object class to form if css_class is specified' do
+ concat(simple_form_for(:user, :html => {:class => nil}) do |f| end)
+ assert_no_select 'form.user'
+ end
+ test 'simple form should add custom class to form if css_class is specified' do
+ concat(simple_form_for(:user, :html => {:class => 'my_class'}) do |f| end)
+ assert_select 'form.my_class'
+ end
test 'pass options to simple form' do
concat(simple_form_for(:user, :url => '/account', :html => { :id => 'my_form' }) do |f| end)

2 comments on commit 1e8bba7


Just wondering - what was the reason for removing the record's type set on html class when you give :class as an HTML option to the simple form for? :-)



It all started when a project I was working on had a model named Alert, and a css style for flash notices existed ".alert"... When using simple form to create an alert, it generated a big huge red box (from the alert class), and there was no way to get rid of this (without renaming the alert css class or name spacing it differently). My argument was that simple_form should give users the control over what css classes are attached to a form.

So if someone has a simple_form for If they specify :html => {:class => "bar"}, it will no longer have a class "foo"... If the person needs that class they just manually need to add it in :html => {:class => "bar foo"}


Please sign in to comment.
Something went wrong with that request. Please try again.