Skip to content

Commit

Permalink
Merge pull request #124 from Fullscreen/fix-error-icon-in-basic-form
Browse files Browse the repository at this point in the history
Correctly align the "X" icon at the right of the field in basic forms
  • Loading branch information
claudiofullscreen committed Mar 11, 2015
2 parents 9036051 + 3f42ef4 commit 98e0743
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ For more information about changelogs, check
[Keep a Changelog](http://keepachangelog.com) and
[Vandamme](http://tech-angels.github.io/vandamme).

## 1.3.3 - 2014-03-11

* [BUGFIX] Correctly align the "X" icon at the right of the field in basic forms

## 1.3.2 - 2014-03-05

* [BUGFIX] Respect the original behavior of Padrino/Rails when calling `link_to` with `nil` as the name
Expand Down
20 changes: 15 additions & 5 deletions lib/bh/core_ext/rails/form/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module BaseHelper

def base_field(method, field_type, options = {}, &block)
errors = (object.errors.get method if object) || {}
label = label_for method, errors, options
label = label_for method, errors, field_type, options
field = field_container(options) do
field_tags errors, field_type, options, &block
end
Expand All @@ -35,7 +35,9 @@ def field_tags(errors, field_type, options = {}, &block)
help_text = options.delete(:help)
field = @template.capture(&block)
tags = [field_in_group(field, prefix, suffix)]
tags << error_icon_tag if show_error_icon?(field_type, errors, suffix)
if show_error_icon?(field_type, errors, suffix) && !basic_form?
tags << error_icon_tag
end
if errors.any? && show_error_help?
tags << help_tag(errors.to_sentence)
elsif help_text
Expand Down Expand Up @@ -86,10 +88,18 @@ def help_tag(help_text)
content_tag :span, help_text, class: klass.join(' ')
end

def label_for(method, errors, options)
# Rails wraps the label in <div class='field_with_errors'> in case of
# error, which messes up the error icon in Bootstrap basic forms unless
# the icon is appended to the label itself, rather than at the end.
def label_for(method, errors, field_type, options)
if options.delete(:use_label) { true }
args = [method, options.delete(:label), label_options(errors)]
label *args.compact
args = [method, options.delete(:label), label_options(errors)]
label(*args.compact).tap do |label|
error_icon = show_error_icon? field_type, errors, options[:suffix]
if (index = label =~ %r{</div>$}) && error_icon && basic_form?
label.insert index, error_icon_tag
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/bh/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bh
VERSION = '1.3.2'
VERSION = '1.3.3'
end

0 comments on commit 98e0743

Please sign in to comment.