Forum: Ruby on Rails How do i switch off error wrapping for a specific field?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-09 13:15
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 :/.
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 Russell Norris (Guest)
on 2007-04-09 17:29
(Received via mailing list)
Or you could just add

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  "<span class=\"error\">#{html_tag}</span>"
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
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-09 17:45
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 ;).
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 Russell Norris (Guest)
on 2007-04-09 18:00
(Received via mailing list)
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
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-09 18:07
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
This topic is locked and can not be replied to.