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.
Dan H. (Guest)
on 2006-03-25 04:22
(Received via mailing list)
Does anyone know how to override the <div class="fieldWithErrors">
behaviour when a form field is incorrect?

Thanks,
Dan
Marcel Molina Jr. (Guest)
on 2006-03-25 04:28
(Received via mailing list)
On Sat, Mar 25, 2006 at 01:22:05PM +1100, Dan H. 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
Dan H. (Guest)
on 2006-03-25 04: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
Dan H. (Guest)
on 2006-03-25 04: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
Ben M. (Guest)
on 2006-03-25 10:17
(Received via mailing list)
Let me guess... inside a td, right?

b
Dan H. (Guest)
on 2006-03-25 13:59
(Received via mailing list)
Ben M. 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
Stefano B. (Guest)
on 2010-05-14 01: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 H. 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
Marnen L. (Guest)
on 2010-05-14 07:37
Dan H. wrote:
> Ben M. 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
removed_email_address@domain.invalid
This topic is locked and can not be replied to.