From 758d09659e707907d2199511569b14811dd141a0 Mon Sep 17 00:00:00 2001 From: Oleg Date: Mon, 15 Jul 2019 12:44:26 -0400 Subject: [PATCH] ability to set errors. closes #19 --- lib/comfy_bootstrap_form/bootstrap_options.rb | 7 ++++ lib/comfy_bootstrap_form/form_builder.rb | 36 +++++++++++-------- .../fields_with_errors_test.rb | 24 +++++++++++++ 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/lib/comfy_bootstrap_form/bootstrap_options.rb b/lib/comfy_bootstrap_form/bootstrap_options.rb index 8cf6e2a..78b510e 100644 --- a/lib/comfy_bootstrap_form/bootstrap_options.rb +++ b/lib/comfy_bootstrap_form/bootstrap_options.rb @@ -69,6 +69,12 @@ class BootstrapOptions # attr_accessor :custom_control + # Manually rendering the error message. Example: + # + # form.text_field :foo, bootstrap: {error: "Error Message"} + # + attr_accessor :error + def initialize(options = {}) set_defaults set_options(options) @@ -124,6 +130,7 @@ def set_defaults @append = nil @prepend = nil @help = nil + @error = nil @check_inline = false @custom_control = false end diff --git a/lib/comfy_bootstrap_form/form_builder.rb b/lib/comfy_bootstrap_form/form_builder.rb index 160a086..8626a7b 100644 --- a/lib/comfy_bootstrap_form/form_builder.rb +++ b/lib/comfy_bootstrap_form/form_builder.rb @@ -76,6 +76,7 @@ def number_field(method, options = {}) bootstrap = form_bootstrap.scoped(options.delete(:bootstrap)) options.reverse_merge!(step: "any") return super(method, options) if bootstrap.disabled + draw_form_group(bootstrap, method, options) do super(method, options) end @@ -130,8 +131,8 @@ def check_box(method, options = {}, checked_value = "1", unchecked_value = "0") bootstrap = form_bootstrap.scoped(options.delete(:bootstrap)) return super if bootstrap.disabled - help_text = draw_help(bootstrap.help) - errors = draw_errors(method) + help_text = draw_help(bootstrap) + errors = draw_errors(bootstrap, method) add_css_class!(options, "form-check-input") add_css_class!(options, "is-invalid") if errors.present? @@ -310,7 +311,7 @@ def form_group(options = {}) # form group wrapper for input fields def draw_form_group(bootstrap, method, options) label = draw_label(bootstrap, method, for_attr: options[:id]) - errors = draw_errors(method) + errors = draw_errors(bootstrap, method) control = draw_control(bootstrap, errors, method, options) do yield @@ -326,15 +327,21 @@ def draw_form_group(bootstrap, method, options) end end - def draw_errors(method) - return unless object.present? + def draw_errors(bootstrap, method) + errors = [] + + if bootstrap.error.present? + errors = [bootstrap.error] + else + return if object.nil? - errors = object.errors[method] + errors = object.errors[method] - # If error os on association like `belongs_to :foo`, we need to render it - # on an input field with `:foo_id` name. - if errors.blank? - errors = object.errors[method.to_s.sub(%r{_id$}, "")] + # If error is on association like `belongs_to :foo`, we need to render it + # on an input field with `:foo_id` name. + if errors.blank? + errors = object.errors[method.to_s.sub(%r{_id$}, "")] + end end return if errors.blank? @@ -410,7 +417,7 @@ def draw_input_group(bootstrap, errors, &block) prepend_html = draw_input_group_content(bootstrap, :prepend) append_html = draw_input_group_content(bootstrap, :append) - help_text = draw_help(bootstrap.help) + help_text = draw_help(bootstrap) # Not prepending or appending anything. Bail. if prepend_html.blank? && append_html.blank? @@ -448,7 +455,8 @@ def draw_input_group_content(bootstrap, type) # # text_field(:value, bootstrap: {help: "help text"}) # - def draw_help(text) + def draw_help(bootstrap) + text = bootstrap.help return if text.blank? content_tag(:small, text, class: "form-text text-muted") @@ -480,8 +488,8 @@ def draw_choices(bootstrap, type, method, collection, value_method, text_method, add_css_class!(html_options, "form-check-input") end - errors = draw_errors(method) - help_text = draw_help(bootstrap.help) + errors = draw_errors(bootstrap, method) + help_text = draw_help(bootstrap) add_css_class!(html_options, "is-invalid") if errors.present? diff --git a/test/comfy_bootstrap_form/fields_with_errors_test.rb b/test/comfy_bootstrap_form/fields_with_errors_test.rb index 1096e02..20f2e48 100644 --- a/test/comfy_bootstrap_form/fields_with_errors_test.rb +++ b/test/comfy_bootstrap_form/fields_with_errors_test.rb @@ -123,4 +123,28 @@ def test_text_field_with_input_group_error assert_xml_equal expected, actual end + def test_text_field_with_manual_error + actual = @builder.text_field(:color, bootstrap: { error: "Custom Error" }) + expected = <<-HTML +
+ + +
Custom Error
+
+ HTML + assert_xml_equal expected, actual + end + + def test_text_field_with_manual_error_overriding + actual = @builder.text_field(:test, bootstrap: { error: "Custom Error" }) + expected = <<-HTML +
+ + +
Custom Error
+
+ HTML + assert_xml_equal expected, actual + end + end