frenzy/lib/frenzy_web/map_form_data.ex

48 lines
930 B
Elixir
Raw Normal View History

defimpl Phoenix.HTML.FormData, for: Map do
alias Phoenix.HTML.Form
def to_form(map, opts) do
{name, opts} = Keyword.pop(opts, :as)
{errors, opts} = Keyword.pop(opts, :errors)
%Form{
source: map,
impl: __MODULE__,
id: name,
name: name,
params: map,
data: %{},
errors: errors,
options: opts
}
end
def to_form(_data, _form, _field, _opts) do
raise "Unimplemented"
end
def input_value(_data, %Form{data: data, params: params}, field) do
key =
case field do
field when is_atom(field) -> Atom.to_string(field)
field -> field
end
case Map.fetch(params, key) do
{:ok, value} ->
value
:error ->
Map.get(data, key)
end
end
def input_validations(_data, _form, _field) do
raise "Unimplemented"
end
def input_type(_data, _form, _field) do
raise "Unimplemented"
end
end