module ActionView
  module Helpers
    module FormHelper
      FIELD_ERROR_OPTIONS = {:tag_name => 'dd', :class => 'FormError'}

      def field_error(object,field,*args)
        options = args.last.is_a?(Hash) ? args.pop : {}
        options = FIELD_ERROR_OPTIONS.merge(options)

        if (errors = object.errors.on(field))
          error_message = case
            when errors.is_a?(String)
              errors
            when errors.is_a?(Array)
              tag(:ul, nil, true) + (errors.collect { |e| content_tag(:li, e) }).to_s + ""
          end

          error_message = %{#{options[:prefix]} #{error_message}} if options[:prefix]

          content_tag(options[:tag_name], error_message, :class => options[:class])
        end
      end
    end
    class FormBuilder
      def field_error(method, options={})
        @template.field_error(@object, method, options)
      end
    end
  end
end