# Moving AR object error wraping to custom FormBuilders

**URL:** <https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942>\
**Category:** Rails\
**Created:** [February 17, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942 "2007-02-17T22:50:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 17, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942/1 "2007-02-17T22:50:32Z")

</div>

Hello,

Previously, I’ve been doing all my active record error feedback in  
forums by using ActionView::Base.field\_error\_proc. However, it is  
somewhat inflexible. I’ve also recently started to “get” custom form  
builders and am liking them. It really feels like the error wrapping  
should go into the form builders in order to obtain the greatest  
amount of ease / flexibility.

I tried to do it this way, but it didn’t come out feeling “right”. I  
was wondering if anybody else has done this or might have some  
suggestions / feedback on what I did.

[http://pastie.caboo.se/41081](http://pastie.caboo.se/41081)

Thanks,  
-carl

–  
EPA Rating: 3000 Lines of Code / Gallon (of coffee)

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 18, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942/2 "2007-02-18T23:56:25Z")

</div>

Nobody has tried to do this before?

On 2/17/07, Carl L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> was wondering if anybody else has done this or might have some  
> suggestions / feedback on what I did.
> 
> [Parked at Loopia](http://pastie.caboo.se/41081)
> 
> Thanks,  
> -carl
> 
> –  
> EPA Rating: 3000 Lines of Code / Gallon (of coffee)

–  
EPA Rating: 3000 Lines of Code / Gallon (of coffee)

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 19, 2007, 3:27am UTC](https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942/3 "2007-02-19T03:27:39Z")

</div>

On Sunday 18 February 2007, Carl L. wrote:

> Nobody has tried to do this before?

[http://www.agilewebdevelopment.com/plugins/label\_helpers](http://www.agilewebdevelopment.com/plugins/label_helpers)

Not a form builder, but you can presumably use the same technique.

HTH,  
Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 21, 2007, 1:11am UTC](https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942/4 "2007-02-21T01:11:21Z")

</div>

Can’t connect to the website of the repository. Is this plugin defunct?

Shauna

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 21, 2007, 2:07am UTC](https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942/5 "2007-02-21T02:07:31Z")

</div>

On Wednesday 21 February 2007, Shauna wrote:

> Can’t connect to the website of the repository. Is this plugin  
> defunct?

I have no idea, but a local copy 🙂  
See below for label\_helper.rb

HTH,  
Michael

[http://www.eric-stewart.com/blog/articles/2006/01/13/label-me-please](http://www.eric-stewart.com/blog/articles/2006/01/13/label-me-please)

# Helpers for generating label tags for inputs

# 

# Includes some support for accessibility techniques

# 

module ActionView  
module Helpers

```
module FormHelper

  # Returns a label tag tailored for describing a specified

```

attribute (identified by +method+) on an object  
# assigned to the template (identified by +object+). Additional  
options on the tag can be passed as a  
# hash with +options+.  
#  
# Examples (call, result):  
# label(“post”, “title”, “Post Title”, :class =\> “foo”)  
# Post Title  
def label(object, method, label\_text = nil, options = {})  
label\_text ||= “#{method.titleize}:”  
InstanceTag.new(object, method, self).to\_label\_tag(label\_text,  
options)  
end

```
end

module FormTagHelper

  # Creates a standard label for a field.
  #
  # Options:
  # * <tt>:disabled</tt> - If set to true, the user will not be able

```

to use this input.  
# \* :hide\_errors - If set to false, the label will append  
validation errors for the object  
# it is a label for  
# A hash of standard HTML options for the tag.  
def label\_tag(for\_id, value = nil, options = {})  
options.stringify\_keys!  
if !options.has\_key?(‘for’)  
options[‘for’] = for\_id  
end  
content\_tag(“label”, value, options)  
end  
end

```
class InstanceTag #:nodoc:

  def to_label_tag(label_text, options = {})
    error_string = ""
    options["for"] ||= "#{tag_id}"
    if options.has_key?('hide_errors')
      options.delete('hide_errors')
    else
      error_string = " <em>#{error_message}</em>"
    end
    content_tag "label", "#{label_text}#{error_string}", options
  end

  # This new error_wrapping method prevents a label that is being

```

generated  
# for an object/field from being wrapped by an error div. Only  
the input  
# tag itself should be wrapped.  
alias\_method :original\_error\_wrapping, :error\_wrapping

```
  def error_wrapping(html_tag, has_error)
    if ['label'].detect { |tag| html_tag.starts_with?("<#{tag}") }
      return html_tag
    end
    original_error_wrapping html_tag, has_error
  end

end

```

end  
end

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![phillyloco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/258eb7/32.png) [@phillyloco](https://www.ruby-forum.com/u/phillyloco)\
**Post date:** [February 21, 2007, 5:53am UTC](https://www.ruby-forum.com/t/moving-ar-object-error-wraping-to-custom-formbuilders/85942/6 "2007-02-21T05:53:12Z")

</div>

Thanks, Michael. Open source is great!

Shauna
