Skip to content

Commit 1a9341e

Browse files
authored
Expand documentation of options_for_select (#460)
1 parent 0d15b13 commit 1a9341e

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

lib/phoenix_html/form.ex

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,27 @@ defmodule Phoenix.HTML.Form do
273273
end
274274

275275
@doc """
276-
Returns options to be used inside a select.
277-
278-
This is useful when building the select by hand.
279-
It expects all options and one or more select values.
276+
Returns options to be used inside a select element.
277+
278+
`options` is expected to be an enumerable which will be used to
279+
generate each `option` element. The function supports different data
280+
for the individual elements:
281+
282+
* keyword lists - each keyword list is expected to have the keys
283+
`:key` and `:value`. Additional keys such as `:disabled` may
284+
be given to customize the option.
285+
* two-item tuples - where the first element is an atom, string or
286+
integer to be used as the option label and the second element is
287+
an atom, string or integer to be used as the option value
288+
* simple atom, string or integer - which will be used as both label and value
289+
for the generated select
290+
291+
## Option groups
292+
293+
If `options` is map or keyword list where the first element is a string,
294+
atom or integer and the second element is a list or a map, it is assumed
295+
the key will be wrapped in an `<optgroup>` and the value will be used to
296+
generate `<options>` nested under the group.
280297
281298
## Examples
282299
@@ -292,7 +309,7 @@ defmodule Phoenix.HTML.Form do
292309
#=> <option value="user">User</option>
293310
#=> <option value="moderator" selected>Moderator</option>
294311
295-
Groups are also supported:
312+
Groups:
296313
297314
options_for_select(["Europe": ["UK", "Sweden", "France"], ...], nil)
298315
#=> <optgroup label="Europe">

0 commit comments

Comments
 (0)