module Padrino::Helpers::FormHelpers::Errors

Helpers to generate form errors.

Public Instance Methods

error_message_on(object, field, options={}) click to toggle source

Returns a string containing the error message attached to the method on the object if one exists.

@param [Object] object

The object to display the error for.

@param [Symbol] field

The field on the +object+ to display the error for.

@param [Hash] options

The options to control the error display.

@option options [String] :tag (“span”)

The tag that encloses the error.

@option options [String] :prepend (“”)

The text to prepend before the field error.

@option options [String] :append (“”)

The text to append after the field error.

@example

# => <span class="error">can't be blank</div>
error_message_on :post, :title
error_message_on @post, :title

# => <div class="custom" style="border:1px solid red">can't be blank</div>
error_message_on :post, :title, :tag => :id, :class => :custom, :style => "border:1px solid red"

# => <div class="error">This title can't be blank (or it won't work)</div>
error_message_on :post, :title, :prepend => "This title", :append => "(or it won't work)"

@return [String] The html display of an error for a particular object and field.

@api public

# File lib/padrino-helpers/form_helpers/errors.rb, line 79
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
error_messages_for(*objects) click to toggle source

Constructs list HTML for the errors for a given symbol.

@overload #error_messages_for(*objects, options = {})

@param [Array<Object>]  object   Splat of objects to display errors for.
@param [Hash]           options  Error message display options.
@option options [String] :header_tag ("h2")
  Used for the header of the error div.
@option options [String] :id ("field-errors")
  The id of the error div.
@option options [String] :class ("field-errors")
  The class of the error div.
@option options [Array<Object>]  :object
  The object (or array of objects) for which to display errors,
  if you need to escape the instance variable convention.
@option options [String] :object_name
  The object name to use in the header, or any text that you prefer.
  If +:object_name+ is not set, the name of the first object will be used.
@option options [String] :header_message ("X errors prohibited this object from being saved")
  The message in the header of the error div. Pass +nil+ or an empty string
  to avoid the header message altogether.
@option options [String] :message ("There were problems with the following fields:")
  The explanation message after the header message and before
  the error list.  Pass +nil+ or an empty string to avoid the explanation message
  altogether.

@return [String] The html section with all errors for the specified objects

@example

error_messages_for :user
# File lib/padrino-helpers/form_helpers/errors.rb, line 39
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