diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md
index 459b6a7fdc..74b612927f 100644
--- a/actionview/CHANGELOG.md
+++ b/actionview/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Alias `ActionView::Helpers::Tags::Label::LabelBuilder#translation` to
+ `#to_s` so that `form.label` calls can yield that value to their blocks.
+
+ *Sean Doyle*
+
* Rename the new `TagHelper#class_names` method to `TagHelper#token_list`,
and make the original available as an alias.
diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb
index 47f966874a..4e681450db 100644
--- a/actionview/lib/action_view/helpers/form_helper.rb
+++ b/actionview/lib/action_view/helpers/form_helper.rb
@@ -1111,6 +1111,16 @@ module ActionView
# label(:post, :privacy, "Public Post", value: "public")
# # =>
#
+ # label(:post, :cost) do |translation|
+ # content_tag(:span, translation, class: "cost_label")
+ # end
+ # # =>
+ #
+ # label(:post, :cost) do |builder|
+ # content_tag(:span, builder.translation, class: "cost_label")
+ # end
+ # # =>
+ #
# label(:post, :terms) do
# raw('Accept Terms.')
# end
@@ -2245,6 +2255,24 @@ module ActionView
# label(:privacy, "Public Post", value: "public")
# # =>
#
+ # label(:cost) do |translation|
+ # content_tag(:span, translation, class: "cost_label")
+ # end
+ # # =>
+ #
+ # label(:cost) do |builder|
+ # content_tag(:span, builder.translation, class: "cost_label")
+ # end
+ # # =>
+ #
+ # label(:cost) do |builder|
+ # content_tag(:span, builder.translation, class: [
+ # "cost_label",
+ # ("error_label" if builder.object.errors.include?(:cost))
+ # ])
+ # end
+ # # =>
+ #
# label(:terms) do
# raw('Accept Terms.')
# end
diff --git a/actionview/lib/action_view/helpers/tags/label.rb b/actionview/lib/action_view/helpers/tags/label.rb
index 436c4cbda3..157fca057e 100644
--- a/actionview/lib/action_view/helpers/tags/label.rb
+++ b/actionview/lib/action_view/helpers/tags/label.rb
@@ -25,6 +25,10 @@ module ActionView
content
end
+
+ def to_s
+ translation
+ end
end
def initialize(object_name, method_name, template_object, content_or_options = nil, options = nil)
diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb
index 162eabb4d5..a24c0b69f5 100644
--- a/actionview/test/template/form_helper/form_with_test.rb
+++ b/actionview/test/template/form_helper/form_with_test.rb
@@ -945,6 +945,56 @@ class FormWithActsLikeFormForTest < FormWithTest
assert_dom_equal expected, output_buffer
end
+ def test_form_with_label_passes_translation_to_block_version
+ form_with(model: Post.new) do |f|
+ concat(
+ f.label(:title) do |label|
+ concat content_tag(:span, label)
+ end
+ )
+ end
+
+ expected = whole_form("/posts") do
+ %()
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_form_with_label_passes_label_tag_builder_to_block_version
+ form_with(model: Post.new) do |f|
+ concat(
+ f.label(:title) do |builder|
+ concat content_tag(:span, builder.translation)
+ end
+ )
+ end
+
+ expected = whole_form("/posts") do
+ %()
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
+ def test_form_with_label_accesses_object_through_label_tag_builder
+ form_with(model: Post.new) do |f|
+ concat(
+ f.label(:title) do |builder|
+ concat tag.span(builder, {
+ class: ("new_record" unless builder.object.persisted?)
+ })
+ end
+ )
+ end
+
+ expected = whole_form("/posts") do
+ %()
+ end
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_form_with_label_error_wrapping
form_with(model: @post) do |f|
concat f.label(:author_name, class: "label")