views:

224

answers:

2

The standard Rails validation wraps my error-filled fields in a div of class fieldWithErrors.

How can I change it so that the erroneous element's parent gets assigned that class instead of having a new div created on the page?

EDIT:

Per this website, this wraps errors in spans instead of divs, which helps my formatting a little, but I'd really love to just stick the error class in the parent element...

app/config/environment.rb (in the initializer block):

config.action_view.field_error_proc = Proc.new { |html_tag, instance| %(<span class="fieldWithErrors">#{html_tag}</span>) }

The Hpricot method listed on that site looks like what I want, but I don't know where I should be putting it. I also keep getting uninitialized constant ActionView errors too. Can someone help?

A: 

You can always just set the divs with the class fieldWithErrors to display inline. You can use CSS instead of trying to ovverride the validation's wrapping method.

CodeJoust
Ehh, not exactly what I'm after; my styling concerns are such that this is not a viable option for me. I know this can be done (see the Hpricot link above; I just added it), I just can't figure out where to place it in my app. Any thoughts?
neezer
A: 

Awesome!

I figured out how to get this post working (I know, I'm such a newbie).

Create a new file in app/config/initializers; I called mine error_formatting.rb.

Paste the optimized method from the website listed above in there:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
   ...
end

Save. Throw in some hpricot with a pinch of config.gem 'hpricot' in the app/config/environment.rb intializer block, then rake gems:install, restart the app, and now my app is working exactly the way I want it too, like a cute little kitten.

neezer