Packages
formex
0.4.13
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.10
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.16
0.4.15
0.4.14
0.4.13
0.4.12
0.4.11
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.3
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Form library for Phoenix with Ecto support
Current section
Files
Jump to
Current section
Files
lib/formex/view_nested.ex
defmodule Formex.View.Nested do
import Formex.View
@moduledoc """
Helper functions for templating nested form.
Check [Type docs](https://hexdocs.pm/formex/Formex.Type.html#module-nested-forms)
for example of use.
"""
@doc false
def formex_nested(form, item_name) do
formex_nested(form, item_name, [], nil)
end
@doc false
def formex_nested(form, item_name, fun) when is_function(fun) do
formex_nested(form, item_name, [], fun)
end
@doc false
def formex_nested(form, item_name, options) when is_list(options) do
formex_nested(form, item_name, options, nil)
end
@doc """
Generates a HTML for nested form
Examples of use:
* Standard
```
<%= formex_nested f, :user_info %>
```
* Set a form template for nested form
```
<div class="form-horizontal">
<%= formex_nested f, :user_info, template: Formex.Template.BootstrapHorizontal %>
</div>
```
* Use your render function
```
<%= formex_nested f, :user_info, fn subform -> %>
<%= formex_row subform, :section %>
<%= formex_row subform, :organisation_cell %>
<% end %>
```
* Template and render function
```
<div class="form-horizontal">
<%= formex_nested f, :user_info, [template: Formex.Template.BootstrapHorizontal],
fn subform -> %>
<%= formex_row subform, :section %>
<%= formex_row subform, :organisation_cell %>
<% end %>
</div>
```
"""
def formex_nested(form, item_name, options \\ [], fun) do
item = Enum.find(form.items, &(&1.name == item_name))
template = Formex.View.get_template(form, options)
template_options = Formex.View.get_template_options(form, options)
if !item do
throw("Key :"<>to_string(item_name)<>" not found in form "<>to_string(form.type))
end
fun = if !fun, do: &(formex_rows(&1)), else: fun
Phoenix.HTML.Form.inputs_for(form.phoenix_form, item.name, fn f ->
item.form
|> Map.put(:phoenix_form, f)
|> Map.put(:template, template)
|> Map.put(:template_options, template_options)
|> fun.()
end)
end
end