Forum: Ruby on Rails Moving AR object error wraping to custom FormBuilders

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.
Carl L. (Guest)
on 2007-02-17 23:50
(Received via mailing list)
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)
Carl L. (Guest)
on 2007-02-19 00:56
(Received via mailing list)
Nobody has tried to do this before?

On 2/17/07, Carl L. <removed_email_address@domain.invalid> wrote:
> 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)
>


--
EPA Rating: 3000 Lines of Code / Gallon (of coffee)
Michael S. (Guest)
on 2007-02-19 04:27
(Received via mailing list)
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:removed_email_address@domain.invalid
http://www.schuerig.de/michael/
Shauna (Guest)
on 2007-02-21 02:11
Can't connect to the website of the repository. Is this plugin defunct?

Shauna
Michael S. (Guest)
on 2007-02-21 03:07
(Received via mailing list)
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/...

# 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")
      #     <label for="post_title" class="foo">Post Title</label>
      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.
      # * <tt>:hide_errors</tt> - 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
http://www.schuerig.de/michael/
Shauna (Guest)
on 2007-02-21 06:53
Thanks, Michael. Open source is great!

Shauna
This topic is locked and can not be replied to.