{"id":510,"date":"2010-12-18T11:51:24","date_gmt":"2010-12-18T04:51:24","guid":{"rendered":"http:\/\/ibatt.info\/web\/?p=510"},"modified":"2011-04-08T10:30:07","modified_gmt":"2011-04-08T03:30:07","slug":"remove-div-fieldwitherrors-from-rails-forms","status":"publish","type":"post","link":"http:\/\/ibatt.info\/web\/?p=510","title":{"rendered":"Remove div.fieldWithErrors from Rails forms"},"content":{"rendered":"<div>\n<p>In a decision I have <em>never<\/em> understood, Rails forms by default add <code>&lt;div&gt; ... &lt;\/div&gt;<\/code> around any field in your form that has validation errors on submission.  Which sucks when you end up with markup like this:<\/p>\n<pre><code>&lt;p&gt;\r\n  &lt;div&gt;&lt;label for=\"post_title\"&gt;Title&lt;\/label&gt;&lt;\/div&gt;&lt;br \/&gt;\r\n  &lt;div&gt;&lt;input id=\"post_title\" name=\"post[title]\" size=\"30\" type=\"text\" value=\"\" \/&gt;&lt;\/div&gt;\r\n&lt;\/p&gt;\r\n<\/code><\/pre>\n<p>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:<\/p>\n<pre><code>ActionView::Base.field_error_proc = Proc.new{ |html_tag, instance| \"&lt;div class=\\\"fieldWithErrors\\\"&gt;#{html_tag}&lt;\/div&gt;\" }\r\n<\/code><\/pre>\n<p>Just override this proc to return the tag only:<\/p>\n<pre><code>ActionView::Base.field_error_proc = proc {|html, instance| html }\r\n<\/code><\/pre>\n<p>In your <code>environment.rb<\/code> file, that would be:<\/p>\n<pre><code>config.action_view.field_error_proc = proc {|html, instance| html }\r\n\r\n\u0e08\u0e32\u0e01 :: <\/code>http:\/\/d.strelau.net\/post\/163547069\/remove-div-fieldwitherrors-from-rails-forms\r\n\r\n<strong>\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e17\u0e35\u0e48\u0e17\u0e33\u0e40\u0e2d\u0e07 \u0e40\u0e2d\u0e32\u0e44\u0e27\u0e49\u0e17\u0e35\u0e48 controller<\/strong>\r\n\r\n ActionView::Base.field_error_proc = proc {|html, instance| %{&lt;span&gt;#{html}&lt;span&gt;&lt;\/span&gt;&lt;\/span&gt;} }<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In a decision I have never understood, Rails forms by default add &lt;div&gt; &#8230; &lt;\/div&gt; around any field in your form that has validation errors on submission. Which sucks when you end up with markup like this: &lt;p&gt; &lt;div&gt;&lt;label for=&#8221;post_title&#8221;&gt;Title&lt;\/label&gt;&lt;\/div&gt;&lt;br \/&gt; &lt;div&gt;&lt;input id=&#8221;post_title&#8221; name=&#8221;post[title]&#8221; size=&#8221;30&#8243; type=&#8221;text&#8221; value=&#8221;&#8221; \/&gt;&lt;\/div&gt; &lt;\/p&gt; At work we?ve had a hack [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"_links":{"self":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts\/510"}],"collection":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=510"}],"version-history":[{"count":4,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions"}],"predecessor-version":[{"id":512,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=\/wp\/v2\/posts\/510\/revisions\/512"}],"wp:attachment":[{"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ibatt.info\/web\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}