# How do i switch off error wrapping for a specific field?

**URL:** <https://www.ruby-forum.com/t/how-do-i-switch-off-error-wrapping-for-a-specific-field/91454>\
**Category:** Rails\
**Created:** [April 9, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/how-do-i-switch-off-error-wrapping-for-a-specific-field/91454 "2007-04-09T13:15:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![skade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@skade](https://www.ruby-forum.com/u/skade)\
**Post date:** [April 9, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/how-do-i-switch-off-error-wrapping-for-a-specific-field/91454/1 "2007-04-09T13:15:59Z")

</div>

Hi, i have some issues with the default rails error wrapping. It wraps  
errors in a div with class ‘fieldsWithError’, which is not good practice  
in my eyes. Adding a class ‘error’ to the field would be much nicer.

My solution to the problem: build your own FormBuilder. Funny enough, i  
found no (nice) possibility to switch error wrapping off while using the  
FormHelper methods. As error\_wrapping is a method in InstanceTag (and  
not of FormHelper), it is pretty hard to have a FormBuilder change its  
behaviour.

One possibility is to define

def initialize(object\_name, object, template, options, proc)  
ActionView::Base.field\_error\_proc = lambda do |html\_tag, instance|  
html\_tag  
end  
super(object\_name, object, template, options, proc)  
end

which works, but i consider it an unclean solution (as it would change  
the error\_proc for the whole interpreter run). Consider the case where i  
use another proc for my fields but the above version of the proc for my  
form - the above version would change the behaviour of all following  
fields.

Another version would be to redefine InstanceTag.error\_wrapping to be  
empty with the same method (not nice, eighter - same Argument).

Is there a nice way to have InstanceTags ignore errors (like  
:ignore\_errors =\> true) so that i can implement them myself (a trivial  
problem inside a FormBuilder). I searched the code, but I find no  
convinient way. Any hints would be much appreciated.

Thanks in advance  
Florian

P.S.: Optional question: Is there a plugin that handles forms like Agavi  
( [www.agavi.org](http://www.agavi.org) )? Aside from it being a PHP Framework (yuk!), I do  
really like the form processing by filtering the output instead of doing  
it explicitly with builders. Customizing forms in rails still seems to  
be a bit of work to me :/.

---

<div class="post-metadata">

**Author:** ![skade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@skade](https://www.ruby-forum.com/u/skade)\
**Post date:** [April 9, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/how-do-i-switch-off-error-wrapping-for-a-specific-field/91454/2 "2007-04-09T17:29:07Z")

</div>

Or you could just add

ActionView::Base.field\_error\_proc = Proc.new do |html\_tag, instance|  
“\<span class=“error”\>#{html\_tag}”  
end

to your config/environment.rb. I agree that using a div [block] where a  
span  
[inline] would work as well seems overkill but I didn’t create Rails.  
And  
granting how awesome all the other stuff is, I think I’ll let this one  
slide. 😉

RSL

---

<div class="post-metadata">

**Author:** ![skade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@skade](https://www.ruby-forum.com/u/skade)\
**Post date:** [April 9, 2007, 5:45pm UTC](https://www.ruby-forum.com/t/how-do-i-switch-off-error-wrapping-for-a-specific-field/91454/3 "2007-04-09T17:45:53Z")

</div>

Well, i already mentioned this ;). However, i found a method that does  
just what i want (leave error handling to me for _just this builder_) it  
may be a bit brutal, too, but it works just as i want:

class ErrorlessFormBuilder \< FormBuilder  
def initialize(object\_name, object, template, options, proc)  
super(object\_name, object, template, options, proc)  
end

def wrap\_proc\_with\_error\_deactivation(proc)  
lambda do |html\_tag, instance|  
old\_proc = ActionView::Base.field\_error\_proc  
ActionView::Base.field\_error\_proc = lambda do |html\_tag, instance|  
html\_tag  
end  
yield proc(html\_tag, instance)  
ActionView::Base.field\_error\_proc = old\_proc  
end  
end  
end

As we do not have to worry about thread safety, this is okay :). Thanks  
for the help.

I do think that rails is great, too ;). It gives me the possibility to  
wrap the form proc in a proc… do that in PHP or Java ;).

---

<div class="post-metadata">

**Author:** ![skade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@skade](https://www.ruby-forum.com/u/skade)\
**Post date:** [April 9, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/how-do-i-switch-off-error-wrapping-for-a-specific-field/91454/4 "2007-04-09T18:00:11Z")

</div>

You know, you _did_ mention that. Only I would have had to actually  
looked  
at your code and not mentally compared the length of it to the length of  
that one I posted. Oopsie?

RSL

---

<div class="post-metadata">

**Author:** ![skade](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@skade](https://www.ruby-forum.com/u/skade)\
**Post date:** [April 9, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/how-do-i-switch-off-error-wrapping-for-a-specific-field/91454/5 "2007-04-09T18:07:23Z")

</div>

Happens ;). Thanks for the bounce, my example code above has an error.  
The constructor has to read:

def initialize(object\_name, object, template, options, proc)  
wrap\_proc\_with\_error\_deactivation(proc)  
super(object\_name, object, template, options, proc)  
end
