diff --git a/README.md b/README.md index 6bd40fda..5d868003 100644 --- a/README.md +++ b/README.md @@ -1573,12 +1573,12 @@ 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 %> ``` @@ -1586,7 +1586,7 @@ Which outputs: ```html
-
+

Please fix the errors below.

  • Email is invalid
  • diff --git a/lib/bootstrap_form/helpers/bootstrap.rb b/lib/bootstrap_form/helpers/bootstrap.rb index 1cf39ce4..7f3d7d3f 100644 --- a/lib/bootstrap_form/helpers/bootstrap.rb +++ b/lib/bootstrap_form/helpers/bootstrap.rb @@ -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 if options[:error_summary] == false title else diff --git a/test/bootstrap_form_test.rb b/test/bootstrap_form_test.rb index 33d04f23..979d9bd1 100644 --- a/test/bootstrap_form_test.rb +++ b/test/bootstrap_form_test.rb @@ -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 +
    +

    Please fix the following errors:

    +
      +
    • Email can't be blank
    • +
    • Email is too short (minimum is 5 characters)
    • +
    • Terms must be accepted
    • +
    +
    + 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?