Forum: Ruby on Rails Overriding <div class="fieldWithErrors">

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
54a6fc785e9fcf73ead8e387432b32f5?d=identicon&s=25 Dan Harper (Guest)
on 2006-03-25 03:22
(Received via mailing list)
Does anyone know how to override the <div class="fieldWithErrors">
behaviour when a form field is incorrect?

Thanks,
Dan
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2006-03-25 03:28
(Received via mailing list)
On Sat, Mar 25, 2006 at 01:22:05PM +1100, Dan Harper wrote:
> Does anyone know how to override the <div class="fieldWithErrors">
> behaviour when a form field is incorrect?

This is what ActionView does:

module ActionView
  class Base
    @@field_error_proc = Proc.new{ |html_tag, instance| "<div
class=\"fieldWithErrors\">#{html_tag}</div>" }
    cattr_accessor :field_error_proc
  end
end

You can set your own Proc. For example, in your config/environment.rb:

ActionView::Base.field_error_proc = Proc.new {|html_tag, instance|
%(<span class="field-with-errors">#{html_tag}</span>)}

marcel
54a6fc785e9fcf73ead8e387432b32f5?d=identicon&s=25 Dan Harper (Guest)
on 2006-03-25 03:29
(Received via mailing list)
Some more information may help.  Having the "div" tags makes my HTML
invalid, so instead a "span" tag would be more appropriate.

Thanks,
Dan
54a6fc785e9fcf73ead8e387432b32f5?d=identicon&s=25 Dan Harper (Guest)
on 2006-03-25 03:38
(Received via mailing list)
Marcel Molina Jr. wrote:
> You can set your own Proc. For example, in your config/environment.rb:
>
> ActionView::Base.field_error_proc = Proc.new {|html_tag, instance|  %(<span 
class="field-with-errors">#{html_tag}</span>)}
>
Cool thanks.  That worked a treat.

Dan
4005a47a8f2ceee49670b920593c1d52?d=identicon&s=25 Ben Munat (Guest)
on 2006-03-25 09:17
(Received via mailing list)
Let me guess... inside a td, right?

b
54a6fc785e9fcf73ead8e387432b32f5?d=identicon&s=25 Dan Harper (Guest)
on 2006-03-25 12:59
(Received via mailing list)
Ben Munat wrote:
> Let me guess... inside a td, right?
>
> b

Actually no, it was inside a <p>.  I was using <p> to group form
sections together.

Dan
Cc997c13216e015a668bba7583213971?d=identicon&s=25 Stefano Benatti (teonimesic)
on 2010-05-13 23:29
It needs a little tweak to make this work on rails 3, since it now
protects all strings against XSS attacks(i.e. html escapes them).

The following works:
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  '<span class="field_with_errors">'.html_safe << html_tag <<
'</span>'.html_safe
end

Obs.: dunno if there isn't a better way of doing this on rails 3.

Marcel Molina Jr. wrote:
> On Sat, Mar 25, 2006 at 01:22:05PM +1100, Dan Harper wrote:
>> Does anyone know how to override the <div class="fieldWithErrors">
>> behaviour when a form field is incorrect?
>
> This is what ActionView does:
>
> module ActionView
>   class Base
>     @@field_error_proc = Proc.new{ |html_tag, instance| "<div
> class=\"fieldWithErrors\">#{html_tag}</div>" }
>     cattr_accessor :field_error_proc
>   end
> end
>
> You can set your own Proc. For example, in your config/environment.rb:
>
> ActionView::Base.field_error_proc = Proc.new {|html_tag, instance|
> %(<span class="field-with-errors">#{html_tag}</span>)}
>
> marcel
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2010-05-14 05:37
Dan Harper wrote:
> Ben Munat wrote:
>> Let me guess... inside a td, right?
>>
>> b
>
> Actually no, it was inside a <p>.  I was using <p> to group form
> sections together.

Well, you don't have to use error_messages_for (which generated the
<div>) to print your errors, and you don't have to put your <div>
somewhere where it will lead to invalid HTML.  <span> is probably
semantically inappropriate.
>
> Dan
Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
This topic is locked and can not be replied to.