Does anyone know how to override the <div class="fieldWithErrors"> behaviour when a form field is incorrect? Thanks, Dan
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 -- Marcel Molina Jr. <marcel@vernix.org>
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 Harper wrote:> Does anyone know how to override the <div class="fieldWithErrors"> > behaviour when a form field is incorrect? > > Thanks, > Dan > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
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
Let me guess... inside a td, right? b Dan Harper wrote:> 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 Harper wrote: > >> Does anyone know how to override the <div class="fieldWithErrors"> >> behaviour when a form field is incorrect? >> >> Thanks, >> Dan >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Ben Munat wrote:> Let me guess... inside a td, right? > > bActually no, it was inside a <p>. I was using <p> to group form sections together. Dan
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-- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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.> > DanBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.