Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1573,20 +1573,20 @@ By default, turning on `:label_errors` will also turn off

To display an error message with an error summary, you can use the
`alert_message` helper. This won't output anything unless a model validation
has failed.
has failed, and additional options will be passed on as attributes to the root `div`:

![Example 49](demo/doc/screenshots/bootstrap/readme/49_example.png "Example 49")
```erb
<%= bootstrap_form_for @user_with_error do |f| %>
<%= f.alert_message "Please fix the errors below." %>
<%= f.alert_message "Please fix the errors below.", tabindex: -1, role: "note" %>
<% end %>
```

Which outputs:

```html
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
<div class="alert alert-danger">
<div class="alert alert-danger" tabindex="-1" role="note">
<p>Please fix the errors below.</p>
<ul class="rails-bootstrap-forms-error-summary">
<li>Email is invalid</li>
Expand Down
2 changes: 1 addition & 1 deletion lib/bootstrap_form/helpers/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def alert_message(title, options={})
css = options[:class] || "alert alert-danger"
return unless object.respond_to?(:errors) && object.errors.full_messages.any?

tag.div class: css do
tag.div class: css, **options.except(:class, :error_summary) do

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

if options[:error_summary] == false
title
else
Expand Down
17 changes: 17 additions & 0 deletions test/bootstrap_form_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,23 @@ def warn(message, ...)
assert_equivalent_html expected, @builder.alert_message("Please fix the following errors:", class: "my-css-class")
end

test "alert_message passes extra options through as HTML attributes" do
@user.email = nil
assert @user.invalid?

expected = <<~HTML
<div class="alert alert-danger" tabindex="-1" role="note">
<p>Please fix the following errors:</p>
<ul class="rails-bootstrap-forms-error-summary">
<li>Email can't be blank</li>
<li>Email is too short (minimum is 5 characters)</li>
<li>Terms must be accepted</li>
</ul>
</div>
HTML
assert_equivalent_html expected, @builder.alert_message("Please fix the following errors:", tabindex: "-1", role: "note")
end

test "alert_message contains the error summary when inline_errors are turned off" do
@user.email = nil
assert @user.invalid?
Expand Down