Module: FormtasticBootstrap::Helpers::ErrorsHelper
- Includes:
- Formtastic::Helpers::FileColumnDetection, Formtastic::Helpers::Reflection, Formtastic::LocalizedString
- Included in:
- FormBuilder
- Defined in:
- lib/formtastic-bootstrap/helpers/errors_helper.rb
Constant Summary collapse
- INLINE_ERROR_TYPES =
[:sentence, :list, :first]
Instance Method Summary collapse
-
#semantic_errors(*args) ⇒ Object
Generates a bootstrap error alert element containing an unordered list of error messages on the base object and optionally for a given set of named attribute.
Instance Method Details
#semantic_errors(*args) ⇒ Object
Generates a bootstrap error alert element containing an unordered list of error messages on the base object and optionally for a given set of named attribute. This is idea for rendering a block of error messages at the top of the form for hidden/special/virtual attributes (the Paperclip Rails plugin does this), or errors on the base model.
A hash can be used as the last set of arguments to pass HTML attributes to the ‘<ul>` wrapper.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/formtastic-bootstrap/helpers/errors_helper.rb', line 42 def semantic_errors(*args) = args. args = args - [:base] full_errors = args.inject([]) do |array, method| attribute = localized_string(method, method.to_sym, :label) || humanized_attribute_name(method) errors = Array(@object.errors[method.to_sym]).to_sentence errors.present? ? array << [attribute, errors].join(" ") : array ||= [] end full_errors << @object.errors[:base] full_errors.flatten! full_errors.compact! return nil if full_errors.blank? if [:class].blank? [:class] = "alert alert-danger" else [:class] = "alert alert-danger " + [:class] end template.content_tag(:div, ) do template.content_tag(:a, "×".html_safe, :class => "close", "data-dismiss" => "alert") + template.content_tag(:ul, {:class => "error-list"}) do Formtastic::Util.html_safe(full_errors.map { |error| template.content_tag(:li, Formtastic::Util.html_safe(error)) }.join) end end end |