Can I set default behavior for empty fields in a Phoenix form to become nil values in changeset (then in database ) instead of “”?
In your controller action you can pattern match, either the function arguments (and create two functions cláusulas) or with a case
, i.e (for a create
action):
def create(conn, %{"your_form_key" => your_form_params}) do
your_form_params =
case your_form_params do
%{"key" => upload} -> #the format you expect
# your code here when not empty
_ ->
Map.put(your_form_params, "key", nil)
end
end
@pedromvieira This sounds like you are looking for scrub_parameters/2
: https://hexdocs.pm/phoenix/Phoenix.Controller.html#scrub_params/2