Skip to content

Commit

Permalink
Merge pull request #445 from parkeryoung/444-render-help-text-in-form…
Browse files Browse the repository at this point in the history
…-group

Issue #444 - Help text rendered in wrong place.
  • Loading branch information
mattbrictson authored Mar 2, 2018
2 parents 824b00f + a89a616 commit ee605d7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
21 changes: 17 additions & 4 deletions lib/bootstrap_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ def form_group(*args, &block)
control = content_tag(:div, control, class: control_class)
end

concat(label).concat(control)
help = options[:help]

help_text = generate_help(name, help).to_s

concat(label).concat(control).concat(help_text)
end
end

Expand Down Expand Up @@ -472,13 +476,22 @@ def generate_label(id, name, options, custom_label_col, group_layout)
end
end

def generate_help(name, help_text)
if has_error?(name) && inline_errors
def has_inline_error?(name)
has_error?(name) && inline_errors
end

def generate_error(name)
if has_inline_error?(name)
help_text = get_error_messages(name)
help_klass = 'invalid-feedback'
help_tag = :div

content_tag(help_tag, help_text, class: help_klass)
end
return if help_text == false
end

def generate_help(name, help_text)
return if help_text == false || has_inline_error?(name)

help_klass ||= 'form-text text-muted'
help_text ||= get_help_text_by_i18n_key(name)
Expand Down
3 changes: 1 addition & 2 deletions lib/bootstrap_form/helpers/bootstrap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ def custom_control(*args, &block)
end

def prepend_and_append_input(name, options, &block)
help = options[:help]
options = options.extract!(:prepend, :append, :input_group_class)
input_group_class = ["input-group", options[:input_group_class]].compact.join(' ')

input = capture(&block) || "".html_safe

input = content_tag(:div, input_group_content(options[:prepend]), class: 'input-group-prepend') + input if options[:prepend]
input << content_tag(:div, input_group_content(options[:append]), class: 'input-group-append') if options[:append]
input << generate_help(name, help).to_s
input << generate_error(name)
input = content_tag(:div, input, class: input_group_class) unless options.empty?
input
end
Expand Down
2 changes: 1 addition & 1 deletion test/bootstrap_form_group_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ class BootstrapFormGroupTest < ActionView::TestCase
<label class="col-form-label col-sm-2 required" for="user_email">Email</label>
<div class="col-sm-10">
<input class="form-control" id="user_email" name="user[email]" type="text" value="[email protected]" />
<small class="form-text text-muted">This is required</small>
</div>
<small class="form-text text-muted">This is required</small>
</div>
HTML
assert_equivalent_xml expected, @horizontal_builder.text_field(:email, help: "This is required")
Expand Down

0 comments on commit ee605d7

Please sign in to comment.