Module: Padrino::Helpers::FormHelpers::Errors

Defined in:
padrino-helpers/lib/padrino-helpers/form_helpers/errors.rb

Overview

Helpers to generate form errors.

Instance Method Summary collapse

Instance Method Details

#error_message_on(object, field, options = {}) ⇒ String

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

Examples:

# => <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)"

Parameters:

  • object (Object)

    The object to display the error for.

  • field (Symbol)

    The field on the object to display the error for.

  • options (Hash) (defaults to: {})

    The options to control the error display.

Options Hash (options):

  • :tag (String) — default: "span"

    The tag that encloses the error.

  • :prepend (String) — default: ""

    The text to prepend before the field error.

  • :append (String) — default: ""

    The text to append after the field error.

Returns:

  • (String)

    The html display of an error for a particular object and field.



79
80
81
82
83
84
85
86
# File 'padrino-helpers/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 ActiveSupport::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(" ")
  (tag, error, options)
end

#error_messages_for(*objects, options = {}) ⇒ String

Constructs list HTML for the errors for a given symbol.

Examples:

error_messages_for :user

Parameters:

  • object (Array<Object>)

    Splat of objects to display errors for.

  • options (Hash) (defaults to: {})

    Error message display options.

Options Hash (options):

  • :header_tag (String) — default: "h2"

    Used for the header of the error div.

  • :id (String) — default: "field-errors"

    The id of the error div.

  • :class (String) — default: "field-errors"

    The class of the error div.

  • :object (Array<Object>)

    The object (or array of objects) for which to display errors, if you need to escape the instance variable convention.

  • :object_name (String)

    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.

  • :header_message (String) — default: "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.

  • :message (String) — default: "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.

Returns:

  • (String)

    The html section with all errors for the specified objects



39
40
41
42
43
44
45
46
# File 'padrino-helpers/lib/padrino-helpers/form_helpers/errors.rb', line 39

def error_messages_for(*objects)
  options = objects.extract_options!.symbolize_keys
  objects = objects.map{ |obj| resolve_object(obj) }.compact
  count   = objects.inject(0){ |sum, object| sum + object.errors.count }
  return ActiveSupport::SafeBuffer.new if count.zero?

  (:div, error_contents(objects, count, options), error_html_attributes(options))
end