mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
html_tag option to wrap error_message_on text [#4283 state:resolved]
Signed-off-by: wycats <wycats@gmail.com>
This commit is contained in:
parent
26bc5c542e
commit
878a9e02f6
2 changed files with 16 additions and 9 deletions
|
@ -97,10 +97,10 @@ module ActionView
|
|||
end
|
||||
|
||||
# Returns a string containing the error message attached to the +method+ on the +object+ if one exists.
|
||||
# This error message is wrapped in a <tt>DIV</tt> tag, which can be extended to include a <tt>:prepend_text</tt>
|
||||
# and/or <tt>:append_text</tt> (to properly explain the error), and a <tt>:css_class</tt> to style it
|
||||
# accordingly. +object+ should either be the name of an instance variable or the actual object. The method can be
|
||||
# passed in either as a string or a symbol.
|
||||
# This error message is wrapped in a <tt>DIV</tt> tag by default or with <tt>:html_tag</tt> if specified,
|
||||
# which can be extended to include a <tt>:prepend_text</tt> and/or <tt>:append_text</tt> (to properly explain
|
||||
# the error), and a <tt>:css_class</tt> to style it accordingly. +object+ should either be the name of an
|
||||
# instance variable or the actual object. The method can be passed in either as a string or a symbol.
|
||||
# As an example, let's say you have a model <tt>@post</tt> that has an error message on the +title+ attribute:
|
||||
#
|
||||
# <%= error_message_on "post", "title" %>
|
||||
|
@ -112,25 +112,28 @@ module ActionView
|
|||
# <%= error_message_on "post", "title",
|
||||
# :prepend_text => "Title simply ",
|
||||
# :append_text => " (or it won't work).",
|
||||
# :html_tag => "span",
|
||||
# :css_class => "inputError" %>
|
||||
# # => <span class="inputError">Title simply can't be empty (or it won't work).</span>
|
||||
def error_message_on(object, method, *args)
|
||||
options = args.extract_options!
|
||||
unless args.empty?
|
||||
ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
|
||||
'prepend_text, append_text, and css_class arguments', caller)
|
||||
'prepend_text, append_text, html_tag, and css_class arguments', caller)
|
||||
|
||||
options[:prepend_text] = args[0] || ''
|
||||
options[:append_text] = args[1] || ''
|
||||
options[:css_class] = args[2] || 'formError'
|
||||
options[:html_tag] = args[2] || 'div'
|
||||
options[:css_class] = args[3] || 'formError'
|
||||
end
|
||||
options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError')
|
||||
options.reverse_merge!(:prepend_text => '', :append_text => '', :html_tag => 'div', :css_class => 'formError')
|
||||
|
||||
object = convert_to_model(object)
|
||||
|
||||
if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
|
||||
(errors = obj.errors[method]).presence
|
||||
content_tag("div",
|
||||
"#{options[:prepend_text]}#{ERB::Util.h(errors.first)}#{options[:append_text]}".html_safe,
|
||||
content_tag(options[:html_tag],
|
||||
(options[:prepend_text].html_safe << errors.first).safe_concat(options[:append_text]),
|
||||
:class => options[:css_class]
|
||||
)
|
||||
else
|
||||
|
|
|
@ -266,6 +266,10 @@ class ActiveModelHelperTest < ActionView::TestCase
|
|||
assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
|
||||
end
|
||||
|
||||
def test_error_message_on_with_tag_option_in_options_hash
|
||||
assert_dom_equal "<span class=\"differentError\">beforecan't be emptyafter</span>", error_message_on(:post, :author_name, :html_tag => "span", :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
|
||||
end
|
||||
|
||||
def test_error_message_on_handles_empty_errors
|
||||
assert_equal "", error_message_on(@post, :tag)
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue