Moving AR object error wraping to custom FormBuilders

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

Thanks,
-carl


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

Nobody has tried to do this before?

On 2/17/07, Carl L. [email protected] wrote:

was wondering if anybody else has done this or might have some
suggestions / feedback on what I did.

Parked at Loopia

Thanks,
-carl


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


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

On Sunday 18 February 2007, Carl L. wrote:

Nobody has tried to do this before?

http://www.agilewebdevelopment.com/plugins/label_helpers

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

HTH,
Michael


Michael S.
mailto:[email protected]
http://www.schuerig.de/michael/

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

Shauna

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 :slight_smile:
See below for label_helper.rb

HTH,
Michael

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:[email protected]
http://www.schuerig.de/michael/

Thanks, Michael. Open source is great!

Shauna