Skip to content

Commit ffa2e82

Browse files
committed
Merge pull request bootstrap-ruby#146 from atipugin/label_errors_fix
Use humanized attribute name in label errors
2 parents 402face + 6479d18 commit ffa2e82

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/bootstrap_form/form_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def generate_label(id, name, options, custom_label_col, group_layout)
307307

308308
if label_errors && has_error?(name)
309309
error_messages = get_error_messages(name)
310-
label_text = (options[:text] || name.to_s.humanize).to_s.concat(" #{error_messages}")
310+
label_text = (options[:text] || object.class.human_attribute_name(name)).to_s.concat(" #{error_messages}")
311311
label(name, label_text, options.except(:text))
312312
else
313313
label(name, options[:text], options.except(:text))

test/bootstrap_form_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ def setup
6363
assert_equal expected, bootstrap_form_for(@user, label_errors: true, inline_errors: true) { |f| f.text_field :email }
6464
end
6565

66+
test "label error messages use humanized attribute names" do
67+
I18n.backend.store_translations(:en, {activerecord: {attributes: {user: {email: 'Your e-mail address'}}}})
68+
69+
@user.email = nil
70+
@user.valid?
71+
72+
expected = %{<form accept-charset=\"UTF-8\" action=\"/users\" class=\"new_user\" id=\"new_user\" method=\"post\" role=\"form\"><div style=\"margin:0;padding:0;display:inline\"><input name=\"utf8\" type=\"hidden\" value=\"&#x2713;\" /></div><div class=\"form-group has-error\"><label class=\"control-label\" for=\"user_email\">Your e-mail address can&#39;t be blank, is too short (minimum is 5 characters)</label><input class=\"form-control\" id=\"user_email\" name=\"user[email]\" type=\"text\" /><span class=\"help-block\">can&#39;t be blank, is too short (minimum is 5 characters)</span></div></form>}
73+
assert_equal expected, bootstrap_form_for(@user, label_errors: true, inline_errors: true) { |f| f.text_field :email }
74+
75+
I18n.backend.store_translations(:en, {activerecord: {attributes: {user: {email: nil}}}})
76+
end
77+
6678
test "alert message is wrapped correctly" do
6779
@user.email = nil
6880
@user.valid?

0 commit comments

Comments
 (0)