fieldWithErrors

Posted on March 25, 2007

I came across a way to overwrite the default fieldWithErrors div that gets created when an input field has an error. Someone else might have already discussed this or has a better way. But I thought I would share what I came up with.

The code that creates the fieldWithErrors div around an input field with errors associated with it is defined in active_record_helper.rb file and it looks like:

  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

In my config/environment.rb file, I overwrite the behaviour like so:

  ActionView::Base.field_error_proc = 
           Proc.new{ |html_tag, instance| "<span class=\"field_with_errors\">#{html_tag}</span>" }