Skip to content

Commit

Permalink
Merge pull request #5691 from avakhov/form-label-block
Browse files Browse the repository at this point in the history
Block version of label should wrapped in field_with_errors in case of error
  • Loading branch information
josevalim committed Apr 1, 2012
2 parents 9228aa6 + 806f4d8 commit eb154c5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions actionpack/lib/action_view/helpers/tags/label.rb
Expand Up @@ -33,7 +33,7 @@ def render(&block)
options["for"] = name_and_id["id"] unless options.key?("for") options["for"] = name_and_id["id"] unless options.key?("for")


if block_given? if block_given?
@template_object.label_tag(name_and_id["id"], options, &block) content = @template_object.capture(&block)
else else
content = if @content.blank? content = if @content.blank?
@object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1') @object_name.gsub!(/\[(.*)_attributes\]\[\d\]/, '.\1')
Expand All @@ -55,9 +55,9 @@ def render(&block)
end end


content ||= @method_name.humanize content ||= @method_name.humanize

label_tag(name_and_id["id"], content, options)
end end

label_tag(name_and_id["id"], content, options)
end end
end end
end end
Expand Down
14 changes: 14 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -1080,6 +1080,20 @@ def test_form_for_label_error_wrapping_without_conventional_instance_variable
assert_dom_equal expected, output_buffer assert_dom_equal expected, output_buffer
end end


def test_form_for_label_error_wrapping_block_and_non_block_versions
form_for(@post) do |f|
concat f.label(:author_name, 'Name', :class => 'label')
concat f.label(:author_name, :class => 'label') { 'Name' }
end

expected = whole_form('/posts/123', 'edit_post_123' , 'edit_post', 'patch') do
"<div class='field_with_errors'><label for='post_author_name' class='label'>Name</label></div>" +
"<div class='field_with_errors'><label for='post_author_name' class='label'>Name</label></div>"
end

assert_dom_equal expected, output_buffer
end

def test_form_for_with_namespace def test_form_for_with_namespace
form_for(@post, :namespace => 'namespace') do |f| form_for(@post, :namespace => 'namespace') do |f|
concat f.text_field(:title) concat f.text_field(:title)
Expand Down

0 comments on commit eb154c5

Please sign in to comment.