How do you remove the default class that simpleform creates on the form?

4,370 views
Skip to first unread message

patrick99e99

unread,
Jul 25, 2011, 3:01:01 PM7/25/11
to SimpleForm
I am doing something like:

simple_form_for @foo

and it is generating something like:

<form class="simple_form foo">
...

I want to be able to customize the class so that it does not include
"foo" in it.... How can I do this?

thank you!

-patrick

Carlos Antonio

unread,
Jul 25, 2011, 3:19:09 PM7/25/11
to plataformate...@googlegroups.com
It's not possible to customize it, SimpleForm add the dom class automatically based on the given object. What you can do is to add more classes by giving the :html => {:class => "bar"} option.

-- 
At.
Carlos A. da Silva

patrick99e99

unread,
Jul 25, 2011, 7:51:43 PM7/25/11
to SimpleForm
Wow.. That is really unfortunate. You really should be able to
control what classes your forms have...

I am on a project where we have a resource "Alert", and also a css
style ".alert" for flash notices.

So, when I try to have a form to create an alert resource, it is
styled as a flash notice!! So now we have to either change the
resource name or change the css classes in the app. It would be so
much nicer if simpleform would let you do something
like :use_dom_class => false

-patrick

Rafael Mendonça França

unread,
Jul 25, 2011, 7:54:40 PM7/25/11
to plataformate...@googlegroups.com
Please, go ahead an send us a pull request. We will be happy to help you with this issue.

-- 
Rafael Mendonça França

patrick99e99

unread,
Jul 25, 2011, 9:04:52 PM7/25/11
to SimpleForm
Ok, pull request sent.

Thank you.

-patrick

Carlos Antonio

unread,
Jul 25, 2011, 9:16:38 PM7/25/11
to plataformate...@googlegroups.com
I'm not completely sure if that should be handled by SimpleForm. We're used to create a wrapper with an id (or sometimes another class) such as a div#flash or div.flash and put the specific messages (notice, alert) inside it. This way I can style them only under the #flash context, avoiding such issues with global classes, solving this.

This way you wouldn't neither need to change your css class names, nor the way SimpleForm handles it. This is just an idea that might help you solving the issue in an easy way and would avoid more changes in SimpleForm, but if you do think it'd be better to change SimpleForm code for that, pls go ahead =). Thanks.

-- 
At.
Carlos A. da Silva

patrick99e99

unread,
Jul 25, 2011, 9:58:32 PM7/25/11
to SimpleForm
Well, the issue for me is that simple_form is not following the same
pattern that rails does when generating a form...

If you do:

form_for User.new do
...
end

that will give you a class "new_user"

and lastly if you do:

form_for User.first do
...
end

that will give you a class "edit_user"... And the most important part
is, you can do:

form_for User.new, :html => {:class => nil} do
...
end

and you will get a form with no class at all. Simple_form's current
functionality, regardless of whether it's a new record or not, will
just give you a class "user". So my objection is to not have control
over the html attributes of my form.

I think a fair compromise would be to replicate rails' form_for class
attribute behavior... That would be something like:

def simple_form_for(record, options={}, &block)
options[:builder] ||= SimpleForm::FormBuilder

options[:html] ||= {}
unless options[:html].key?(:novalidate)
options[:html][:novalidate] = !SimpleForm.browser_validations
end
options[:html][:class] = [SimpleForm.form_class,
css_class(record, html_options)].compact.join(" ")

with_custom_field_error_proc do
form_for(record, options, &block)
end
end

def css_class(record, html_options)
record = record.last if record.is_a?(Array)

if html_options.has_key?(:class)
html_options[:class]
else
dom_class(record, (record.new_record? ? :new : :edit))
end
end

So this way, if someone uses an array like,

[:person, :posts, @comment], it will still function properly and give
a new_comment or edit_comment class...

The only behavior this is lacking is if a user passes in a string..
simple_form_for "User" do... But in this case, I think if they need a
specific user class, they can just pass it in as the css option...

What do you guys think? If you like this I will be happy to package
it up on a new branch and write some tests and do another pull
request.

-patrick

On Jul 25, 6:16 pm, Carlos Antonio <carlosantoniodasi...@gmail.com>
wrote:
> I'm not completely sure if that should be handled by SimpleForm. We're used to create a wrapper with an id (or sometimes another class) such as a div#flash or div.flash and put the specific messages (notice, alert) inside it. This way I can style them only under the #flash context, avoiding such issues with global classes, solving this.
>
> This way you wouldn't neither need to change your css class names, nor the way SimpleForm handles it. This is just an idea that might help you solving the issue in an easy way and would avoid more changes in SimpleForm, but if you do think it'd be better to change SimpleForm code for that, pls go ahead =). Thanks.  
>
> --  
> At.
> Carlos A. da Silva
>
>
>
>
>
>
>
> On Monday, July 25, 2011 at 8:54 PM, Rafael Mendonça França wrote:
> > Please, go ahead an send us a pull request. We will be happy to help you with this issue.
>
> > --  
> > Rafael Mendonça França
> > Software developer at Plataformatec
> >http://twitter.com/rafaelfranca
> >https://github.com/rafaelfranca
> > Sent with Sparrow (http://www.sparrowmailapp.com)

Frank Tellefsen

unread,
Feb 8, 2015, 9:55:07 PM2/8/15
to plataformate...@googlegroups.com

Is this still the case? Would really like to disable the form class so it doesn't conflict with my CSS.

Came across http://stackoverflow.com/questions/7809292/rails-simple-form-disable-form-class as well.

Thanks!

Frank

Carlos Antonio da Silva

unread,
Feb 9, 2015, 4:38:42 AM2/9/15
to plataformate...@googlegroups.com
Latest Simple Form versions have deprecated the form_class option in favor of default_form_class, which you can turn off by passing the :class option: see more in https://github.com/plataformatec/simple_form/pull/1109.

--
You received this message because you are subscribed to the Google Groups "SimpleForm" group.
To unsubscribe from this group and stop receiving emails from it, send an email to plataformatec-simp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Frank Tellefsen

unread,
Mar 24, 2015, 4:30:10 PM3/24/15
to plataformate...@googlegroups.com
Hi!

So basically I can set `config.default_form_class = nil` in `config/initializers/simple_form.rb` instead of having to put `simple_form_for @thing, html: { class: nil }` for each of my forms?

Thank you!

Frank

Frank Tellefsen

unread,
Mar 24, 2015, 4:41:57 PM3/24/15
to plataformate...@googlegroups.com

Frank Tellefsen

unread,
Apr 27, 2015, 5:21:25 AM4/27/15
to plataformate...@googlegroups.com
Hello!

So I tried replacing `simple_form_for @thing, html: { class: nil }` with `config.default_form_class = nil` in `config/initializers/simple_form.rb` using Simple Form 3.1.0 but now it doesn't turn off my classes anymore.

Thoughts welcome.

Thanks!

Frank

Frank Tellefsen

unread,
Apr 30, 2015, 10:35:23 AM4/30/15
to plataformate...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages