Black ribbon bottom left

Modify div.fieldWithErrors from Rails forms

In a decision I have never understood, Rails forms by default add <div> ... </div> around any field in your form that has validation errors on submission. Which sucks when you end up with markup like this:

<div><label for="post_title">Title</label></div><br />
<div><input id="post_title" name="post[title]" size="30" type="text" value="" /></div>

At work we’ve had a hack in place for a while not that dug into ActionView and turned off this nonsense. We normally don’t go highlighting form fields with errors anyway. As it turns out though, that HTML is actually rendered by a proc you can set. By default it looks like this:

ActionView::Base.field_error_proc ={ |html_tag, instance| "<div class=\"fieldWithErrors\">#{html_tag}</div>" }

Just override this proc to return the tag only:

ActionView::Base.field_error_proc = proc {|html, instance| html }

In your environment.rb file, that would be:

config.action_view.field_error_proc = proc {|html, instance| html }

จาก ::

ตัวอย่างที่ทำเอง เอาไว้ที่ controller

ActionView::Base.field_error_proc = proc {|html, instance| %{<span>#{html}<span></span></span>} }