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

### Home Classes Methods

### In Files

- lib/padrino-helpers/form_helpers/errors.rb

### Methods

- #error_message_on
- #error_messages_for

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Padrino
- Padrino::Helpers
- Padrino::Helpers::AssetTagHelpers
- Padrino::Helpers::FormBuilder
- Padrino::Helpers::FormBuilder::AbstractFormBuilder
- Padrino::Helpers::FormBuilder::DeprecatedBuilderMethods
- Padrino::Helpers::FormBuilder::StandardFormBuilder
- Padrino::Helpers::FormHelpers
- Padrino::Helpers::FormHelpers::Errors
- Padrino::Helpers::FormHelpers::Options
- Padrino::Helpers::FormHelpers::Security
- Padrino::Helpers::NumberHelpers
- Padrino::Helpers::OutputHelpers
- Padrino::Helpers::OutputHelpers::AbstractHandler
- Padrino::Helpers::OutputHelpers::ErbHandler
- Padrino::Helpers::OutputHelpers::HamlHandler
- Padrino::Helpers::OutputHelpers::SinatraCurrentEngine
- Padrino::Helpers::OutputHelpers::SlimHandler
- Padrino::Helpers::RenderHelpers
- Padrino::Helpers::TagHelpers
- Padrino::Helpers::TranslationHelpers
- Padrino::Rendering
- Padrino::Rendering::ClassMethods
- Padrino::Rendering::ErubisTemplate
- Padrino::Rendering::HamlTemplate
- Padrino::Rendering::InstanceMethods
- Padrino::Rendering::SafeBufferEnhancer
- Padrino::Rendering::SafeEruby
- Padrino::Rendering::SafeTemplate
- Padrino::Rendering::SlimTemplate
- Padrino::Rendering::TemplateNotFound
- Haml
- Haml::Helpers
- Haml::Util

No matching classes.

# 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 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(" ")
      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.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?

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

[Validate]

Generated with the Darkfish Rdoc Generator 2.