diff --git a/lib/phoenix_html/form.ex b/lib/phoenix_html/form.ex
index 190a5a02..574fb819 100644
--- a/lib/phoenix_html/form.ex
+++ b/lib/phoenix_html/form.ex
@@ -1433,6 +1433,9 @@ defmodule Phoenix.HTML.Form do
"E-mail Address"
end
#=>
+
+ label :user, :newsletters, "Recieve all newsletters", value: "all"
+ #=>
"""
def label(form, field) do
label(form, field, humanize(field), [])
@@ -1457,15 +1460,25 @@ defmodule Phoenix.HTML.Form do
See `label/2`.
"""
def label(form, field, text, opts) when is_binary(text) and is_list(opts) do
- opts = Keyword.put_new(opts, :for, input_id(form, field))
+ opts = label_options(form, field, opts)
content_tag(:label, text, opts)
end
def label(form, field, opts, do: block) do
- opts = Keyword.put_new(opts, :for, input_id(form, field))
+ opts = label_options(form, field, opts)
content_tag(:label, opts, do: block)
end
+ defp label_options(form, field, opts) do
+ {opts, input_id} =
+ case Keyword.pop(opts, :value) do
+ {nil, opts} -> {opts, input_id(form, field)}
+ {value, opts} -> {opts, input_id(form, field, value)}
+ end
+
+ Keyword.put_new(opts, :for, input_id)
+ end
+
# Normalize field name to string version
defp field_to_string(field) when is_atom(field), do: Atom.to_string(field)
defp field_to_string(field) when is_binary(field), do: field
diff --git a/test/phoenix_html/form_test.exs b/test/phoenix_html/form_test.exs
index ec1228b0..dc76e634 100644
--- a/test/phoenix_html/form_test.exs
+++ b/test/phoenix_html/form_test.exs
@@ -1173,6 +1173,10 @@ defmodule Phoenix.HTML.FormTest do
~s()
end
+ test "label/4 with for_value option" do
+ assert safe_form(&label(&1, :key, value: "1")) == ~s()
+ end
+
test "label/3 with a block" do
assert safe_form(&label(&1, :key, do: "Hello")) == ~s()
end