module Padrino::Helpers::FormHelpers::Errors

def error_body_tag(options)

def error_body_tag(options)
  body_message = options[:message] || I18n.t(:body, :locale => options[:locale], :scope => [:models, :errors, :template])
  content_tag(:p, body_message) unless body_message.empty?
end

def error_contents(objects, count, options)

def error_contents(objects, count, options)
  object_name = options[:object_name] || Inflections.underscore(objects.first.class).gsub(/\//, ' ')
  contents = SafeBuffer.new
  contents << error_header_tag(options, object_name, count)
  contents << error_body_tag(options)
  contents << error_list_tag(objects, object_name)
end

def error_header_tag(options, object_name, count)

def error_header_tag(options, object_name, count)
  header_message = options[:header_message] || begin
    model_name = I18n.t(:name, :default => Inflections.humanize(object_name), :scope => [:models, object_name], :count => 1)
    I18n.t :header, :count => count, :model => model_name, :locale => options[:locale], :scope => [:models, :errors, :template]
  end
  content_tag(options[:header_tag] || :h2, header_message) unless header_message.empty?
end

def error_html_attributes(options)

def error_html_attributes(options)
  [:id, :class, :style].each_with_object({}) do |key,all|
    if options.include?(key)
      value = options[key]
      all[key] = value if value
    else
      all[key] = 'field-errors' unless key == :style
    end
  end
end

def error_list_tag(objects, object_name)

def error_list_tag(objects, object_name)
  errors = objects.inject({}){ |all,object| all.update(object.errors) }
  error_messages = errors.inject(SafeBuffer.new) do |all, (field, message)|
    field_name = I18n.t(field, :default => Inflections.humanize(field), :scope => [:models, object_name, :attributes])
    all << content_tag(:li, "#{field_name} #{message}")
  end
  content_tag(:ul, error_messages)
end

def error_message_on(object, field, options={})

Other tags:
    Api: - public

Returns:
  • (String) - The html display of an error for a particular +object+ and +field+.

Options Hash: (**options)
  • :append (String) --
  • :prepend (String) --
  • :tag (String) --

Parameters:
  • options (Hash) --
  • field (Symbol) --
  • object (Object) --
def error_message_on(object, field, options={})
  error = Array(resolve_object(object).errors[field]).first
  return SafeBuffer.new unless error
  options = { :tag => :span, :class => :error }.update(options)
  tag   = options.delete(:tag)
  error = [options.delete(:prepend), error, options.delete(:append)].compact.join(" ")
  content_tag(tag, error, options)
end

def error_messages_for(*objects)

Returns:
  • (String) - The html section with all errors for the specified +objects+

Options Hash: (**options)
  • :message (String) --
  • :header_message (String) --
  • :object_name (String) --
  • :object (Array) --
  • :class (String) --
  • :id (String) --
  • :header_tag (String) --

  • Parameters:
    • options (Hash) -- Error message display options.
    • object (Array) -- Splat of objects to display errors for.
      Overloads:
      • error_messages_for(*objects, options = {})
      def error_messages_for(*objects)
        options = objects.last.is_a?(Hash) ? Utils.symbolize_keys(objects.pop) : {}
        objects = objects.map{ |obj| resolve_object(obj) }.compact
        count   = objects.inject(0){ |sum, object| sum + object.errors.count }
        return SafeBuffer.new if count.zero?
        content_tag(:div, error_contents(objects, count, options), error_html_attributes(options))
      end

      def resolve_object(object)

      def resolve_object(object)
        object.is_a?(Symbol) ? instance_variable_get("@#{object}") : object
      end