If I understand your question correctly, you are using something like this in your forms:
<%= f.semantic_errors :bar %>
To change the behavior of semantic_errors , you can defuse this method. To do this, simply add the file {RAILS_ROOT}/config/initializers/semantic_errors_patch.rb with the content:
Formtastic::Helpers::ErrorsHelper.class_eval do def semantic_errors(*args) html_options = args.extract_options! 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? html_options[:class] ||= "errors" template.content_tag(:ul, html_options) do Formtastic::Util.html_safe(full_errors.map { |error| template.content_tag(:li, Formtastic::Util.html_safe(error)) }.join) end end end
This patch works well with formtastic 2.2.1 and rails 3.2.13 .
This patch will cause the following line for two errors:
Bar: sie haben Ihr Schnitzel vergessen und sie haben Ihr Bier vergessen.
If there are more errors, this will produce something like:
Amount: not a number, cannot be empty and too short (minimum 2 characters)
You can specify this behavior on this line:
errors = Array(@object.errors[method.to_sym]).to_sentence
@object.errors[method.to_sym] is a set of errors that produce the final string of errors .
Viacheslav Molokov
source share