Remove div.fieldWithErrors from Rails forms

Source: [url]http://d.strelau.net/post/163547069/remove-div-fieldwitherrors-from-rails-forms[/url]

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

<p>
<div class="fieldWithErrors"><label for="post_title">Title</label></div><br />
<div class="fieldWithErrors"><input id="post_title" name="post[title]" size="30" type="text" value="" /></div>
</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:

ActionView::Base.field_error_proc = Proc.new{ |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 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值