Forum: Ruby on Rails can error_message_on display multiple errors?

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.
Scott N. (Guest)
on 2009-01-11 06:46
(Received via mailing list)
Is there an easy way to make error_message_on display more than just
one error message? I know I could create a custom form builder, but it
looks complicated and I would rather learn that when I have more time.
Scott N. (Guest)
on 2009-01-12 01:19
(Received via mailing list)
for anyone else who might come across this, i found field_error_proc.
you can over ride it and have it automatically add the errors, you can
leave the error_message_on out of your code completely. you just add
this code to an initializer.

i combined it with the second example to also stop rails from putting
those annoying divs around the fields with errors, it makes much more
sense to simply add a css class to the fields.

http://snippets.dzone.com/posts/show/1671

ActionView::Base.field_error_proc = Proc.new {|html_tag, instance|
  if instance.error_message.kind_of?(Array)
    %(#{html_tag}<span class='validation-error'>&larr;#
{instance.error_message.join(',')}</span>)
  else
    %(#{html_tag}<span class='validation-error'>&larr;#
{instance.error_message}</span>)
  end
  }

http://thewebfellas.com/blog/2008/4/21/error-field...

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  if html_tag =~ /<(input|label|textarea|select)/
    error_class = 'error'
    nodes = Hpricot(html_tag)
    nodes.each_child { |node| node[:class] = node.classes.push
(error_class).join(' ') unless !node.elem? || node[:type] == 'hidden'
|| node.classes.include?(error_class) }
    nodes.to_html
  else
    html_tag
  end
end
This topic is locked and can not be replied to.