Changing the Rails field_error_proc on a per-controller or per-action basis

So we know we can change how rails displays errors in forms with fields that fail validation. I came across a reason to need to change the ActionView::Base.field_error_proc temporarily, and then set it back… sort of like having different values for the field_error_proc in different places. I came across this post that basically uses a helper method to store the old field_proc_error value, change it, then change it back. Realizing that I could do that brought me to a solution that works better in my case.

My solution is to use an around filter on my controller and a custom class. This way, I can set how form validation errors are displayed on entire controllers or certain actions.

I created a new file called field_error_proc_changer.rb in my lib directory as follows:

class FieldErrorProcChanger
  def initialize(proc)
    @new_proc = proc
  end

  # This will run before the action. Returning false
  # aborts the action.
  def before(controller)
    @old_proc = ActionView::Base.field_error_proc
    ActionView::Base.field_error_proc = @new_proc
    true
  end

  # This will run after the action if and only if
  # before returned true.
  def after(controller)
    ActionView::Base.field_error_proc = @old_proc
  end
end

Now, you can replace the existing field_error_proc using a normal around_filter

class RandomController < ApplicationController

  around_filter FieldErrorProcChanger.new(
    Proc.new {|html_tag, instance| "#{html_tag}"}
  )

  def index
    foo = bar
  end
end

Or on only certain actions like

  around_filter FieldErrorProcChanger.new(
    Proc.new {|html_tag, instance| "#{html_tag}"}
  ), :o nly => [:index]

The value for field_error_proc I’m showing actually doesn’t add anything to the field with validation errors (I’m using a custom form builder that adds special classes to fields with errors, so I don’t need any html added). But, you could add a fancier proc too:

around_filter FieldErrorProcChanger.new(
  Proc.new  do |html_tag, instance|
    html = &#8220;&#8221;
    html << %{<div class="ridiculous_construct">}
    html << %{<span>There&#8217;s an error over here!</span>}
    html << %{#{html_tag}</div>}
  end
)

Why would you want to do this? In my case, I’m adding an extension to the Radiant CMS. My extension has an administrative interface, and my forms aren’t exactly like the default Radiant forms. Radiant sets its own field_error_proc, which I don’t want in my controllers, but I don’t want to change the way the native Radiant admin works or looks.

This entry was posted in Home and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.