Skip to content

Commit

Permalink
ability to set errors. closes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
GBH committed Jul 15, 2019
1 parent 304b6a9 commit 758d096
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
7 changes: 7 additions & 0 deletions lib/comfy_bootstrap_form/bootstrap_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -124,6 +130,7 @@ def set_defaults
@append = nil
@prepend = nil
@help = nil
@error = nil
@check_inline = false
@custom_control = false
end
Expand Down
36 changes: 22 additions & 14 deletions lib/comfy_bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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?

Expand Down
24 changes: 24 additions & 0 deletions test/comfy_bootstrap_form/fields_with_errors_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
<div class="form-group">
<label for="user_color">Color</label>
<input class="form-control is-invalid" id="user_color" name="user[color]" type="text"/>
<div class="invalid-feedback">Custom Error</div>
</div>
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
<div class="form-group">
<label for="user_test">Test</label>
<input class="form-control is-invalid" id="user_test" name="user[test]" type="text"/>
<div class="invalid-feedback">Custom Error</div>
</div>
HTML
assert_xml_equal expected, actual
end

end

0 comments on commit 758d096

Please sign in to comment.