mirror of https://github.com/rails/rails
Do not wrap hidden fields with error proc [#4962 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
617e94658d
commit
b01df28413
|
@ -36,12 +36,16 @@ module ActionView
|
|||
end
|
||||
end
|
||||
|
||||
%w(tag content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
|
||||
%w(content_tag to_date_select_tag to_datetime_select_tag to_time_select_tag).each do |meth|
|
||||
module_eval "def #{meth}(*) error_wrapping(super) end", __FILE__, __LINE__
|
||||
end
|
||||
|
||||
def tag(type, options, *)
|
||||
tag_generate_errors?(options) ? error_wrapping(super) : super
|
||||
end
|
||||
|
||||
def error_wrapping(html_tag)
|
||||
if object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && object.errors[@method_name].any?
|
||||
if object_has_errors?
|
||||
Base.field_error_proc.call(html_tag, self)
|
||||
else
|
||||
html_tag
|
||||
|
@ -51,6 +55,16 @@ module ActionView
|
|||
def error_message
|
||||
object.errors[@method_name]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def object_has_errors?
|
||||
object.respond_to?(:errors) && object.errors.respond_to?(:full_messages) && error_message.any?
|
||||
end
|
||||
|
||||
def tag_generate_errors?(options)
|
||||
options['type'] != 'hidden'
|
||||
end
|
||||
end
|
||||
|
||||
class FormBuilder
|
||||
|
|
|
@ -124,7 +124,6 @@ module ActionView
|
|||
# model:
|
||||
#
|
||||
# <%= form_for :person do |f| %>
|
||||
# <%= f.error_messages %>
|
||||
# First name: <%= f.text_field :first_name %><br />
|
||||
# Last name : <%= f.text_field :last_name %><br />
|
||||
# Biography : <%= f.text_area :biography %><br />
|
||||
|
|
|
@ -39,6 +39,13 @@ class ActiveModelHelperTest < ActionView::TestCase
|
|||
)
|
||||
end
|
||||
|
||||
def test_hidden_field_does_not_render_errors
|
||||
assert_dom_equal(
|
||||
%(<input id="post_author_name" name="post[author_name]" type="hidden" value="" />),
|
||||
hidden_field("post", "author_name")
|
||||
)
|
||||
end
|
||||
|
||||
def test_field_error_proc
|
||||
old_proc = ActionView::Base.field_error_proc
|
||||
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
|
||||
|
|
Loading…
Reference in New Issue