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


#1

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 )? 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 :/.


#2

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. :wink:

RSL


#3

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 ;).


#4

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


#5

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