<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Michael Klett &#187; radiant</title>
	<atom:link href="http://michaelklett.com/tag/radiant/feed/" rel="self" type="application/rss+xml" />
	<link>http://michaelklett.com</link>
	<description></description>
	<lastBuildDate>Sun, 27 Sep 2009 18:12:14 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Changing the Rails field_error_proc on a per-controller or per-action basis</title>
		<link>http://michaelklett.com/2008/02/02/changing-the-rails-field_error_proc-on-a-per-controller-or-per-action-basis/</link>
		<comments>http://michaelklett.com/2008/02/02/changing-the-rails-field_error_proc-on-a-per-controller-or-per-action-basis/#comments</comments>
		<pubDate>Sat, 02 Feb 2008 23:09:00 +0000</pubDate>
		<dc:creator>Michael Klett</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[radiant]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://michaelklett.com/2008/02/03/changing-the-rails-field_error_proc-on-a-per-controller-or-per-action-basis</guid>
		<description><![CDATA[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&#8230; sort of like having different values for the field_error_proc in different places.  I came across this post that basically [...]]]></description>
			<content:encoded><![CDATA[<p>So we know <a href="http://www.jroller.com/obie/entry/customizing_how_rails_validation_errors">we can change how rails displays errors</a> in forms with fields that fail validation.  I came across a reason to need to change the <code>ActionView::Base.field_error_proc</code> temporarily, and then set it back&#8230; sort of like having different values for the <code>field_error_proc</code> in different places.  I came across <a href="http://pivots.pivotallabs.com/users/felix/blog/articles/267-applying-different-error-display-styles">this post</a> that basically uses a helper method to store the old <code>field_proc_error</code> value, change it, then change it back.  Realizing that I could do that brought me to a solution that works better in my case.</p>
<p>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.</p>
<p>I created a new file called <code>field_error_proc_changer.rb</code> in my <code>lib</code> directory as follows:</p>
<pre><code>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
</code></pre>
<p>Now, you can replace the existing <code>field_error_proc</code> using a normal <a href="http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html#M000316">around_filter</a></p>
<pre><code>class RandomController &lt; ApplicationController

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

  def index
    foo = bar
  end
end
</code></pre>
<p>Or on only certain actions like</p>
<pre><code>  around_filter FieldErrorProcChanger.new(
    Proc.new {|html_tag, instance| "#{html_tag}"}
  ), <img src='http://michaelklett.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> nly =&gt; [:index]
</code></pre>
<p>The value for <code>field_error_proc</code> I&#8217;m showing actually doesn&#8217;t add anything to the field with validation errors (I&#8217;m using a custom form builder that adds special classes to fields with errors, so I don&#8217;t need any html added).  But, you could add a fancier proc too:</p>
<pre><code>around_filter FieldErrorProcChanger.new(
  Proc.new  do |html_tag, instance|
    html = &amp;#8220;&amp;#8221;
    html &lt;&lt; %{&lt;div class="ridiculous_construct"&gt;}
    html &lt;&lt; %{&lt;span&gt;There&amp;#8217;s an error over here!&lt;/span&gt;}
    html &lt;&lt; %{#{html_tag}&lt;/div&gt;}
  end
)
</code></pre>
<p>Why would you want to do this?  In my case, I&#8217;m adding an extension to the Radiant CMS. My extension has an administrative interface, and my forms aren&#8217;t exactly like the default Radiant forms.  Radiant sets its own <code>field_error_proc</code>, which I don&#8217;t want in my controllers, but I don&#8217;t want to change the way the native Radiant admin works or looks.</p>
]]></content:encoded>
			<wfw:commentRss>http://michaelklett.com/2008/02/02/changing-the-rails-field_error_proc-on-a-per-controller-or-per-action-basis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
