Skip to content


Subversion checkout URL

You can clone with
Download ZIP


I added a config option to show attribute names by errors #922

wants to merge 9 commits into from

3 participants


This is a follow up to my issue post: #919

If this gets accepted I'll include the functionality in formtastic-bootstrap ASAP and put in another pull request for that. I've tried my best to do this touching as little code as possible.


I think errors() should not take an options hash. You have access to the builder/configuration, and everywhere you're calling errors has been changed to errors(error_options), so moving to an options hash feels unnecessary from what I've seen. If it becomes necessary, I'd prefer to see a separate method.

I think it'd also be helpful for us in the future to document the reason for adding this too (in this PR will do), for future reference. Is it to satisfy a Bootstrap pattern, or just personal preference?


It's a bit of both if I'm honest, I tend to set the error text to be a bit smaller than the input labels, and it just looks untidy to me that it doesn't show the input name next to the error, to the point that I was convinced it wasn't intentional.

I'll remove the options hash, I had passed it along to avoid appending the attribute names on all errors if we were just taking the first, but I guess it introduces more complexity than it cuts down on overhead. Working on an updated commit now.


For me this makes sense because I have design where the label has been hidden in the design.


This needs some spec coverage, but I'm happy to merge it in after that. Will close down if there's no activity after a month or so.


Closing due to inactivity, happy to merge with spec coverage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  lib/formtastic/form_builder.rb
@@ -15,6 +15,7 @@ def self.configure(name, value = nil)
configure :required_string, proc { Formtastic::Util.html_safe(%{<abbr title="#{Formtastic::I18n.t(:required)}">*</abbr>}) }
configure :optional_string, ''
configure :inline_errors, :sentence
+ configure :attribute_names_on_errors, false
configure :label_str_method, :humanize
configure :collection_label_methods, %w[to_label display_name full_name name title username login value to_s]
configure :collection_value_methods, %w[id to_s]
12 lib/formtastic/inputs/base/errors.rb
@@ -14,10 +14,12 @@ def error_sentence_html
def error_list_html
error_class = options[:error_class] || builder.default_error_list_class
list_elements = []
errors.each do |error|
list_elements << template.content_tag(:li, Formtastic::Util.html_safe(error.html_safe))
template.content_tag(:ul, Formtastic::Util.html_safe(list_elements.join("\n")), :class => error_class)
@@ -38,7 +40,13 @@ def errors
errors = []
if object && object.respond_to?(:errors)
error_keys.each do |key|
- errors << object.errors[key] unless object.errors[key].blank?
+ unless object.errors[key].blank?
+ if builder.attribute_names_on_errors
+ errors << object.errors[key] { |err, idx| (idx==0) ? "#{key.to_s.titleize} #{err}" : err }
+ else
+ errors << object.errors[key]
+ end
+ end
@@ -50,7 +58,7 @@ def error_keys
keys << association_primary_key if belongs_to? || has_many?
5 lib/generators/templates/formtastic.rb
@@ -74,3 +74,8 @@
# this to true. Doing so will add a `novalidate` attribute to the `<form>` tag.
# See for more info.
# Formtastic::FormBuilder.perform_browser_validations = true
+# You can set attribute names to show at the beginning of error messages by setting
+# this to true. For example, 'must not be blank' would become "Title must not be blank" or
+# 'Name must not be blank'.
+# Formtastic::Formbuilder.attribute_names_on_errors = false
Something went wrong with that request. Please try again.