You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the Phoenix documentation, if no file is selected, Plug.Upload will not be present in the params map.
Finally, notice that when there is no data from the file input, we get neither the "photo" key nor a Plug.Upload struct. Here are the user_params from the log.
So if you submit a blank form, params[:user] will be nil. For example:
What's the point of |> validate_required([:avatar]) given that Plug.Upload is only present with file data? To reach this point in the changeset, a field must be present. It seems redundant then to validate for presence.
How do we validate the presence of avatar?
Here's my user schema:
defmodule App.User do
use App.Web, :model
use Arc.Ecto.Schema
schema "users" do
# ...
field :avatar, App.AvatarUploader.Type
end
def avatar_changeset(struct, params \\ %{}) do
struct
|> cast(params, [:avatar])
|> cast_attachments(params, [:avatar])
|> validate_required([:avatar])
end
Am I missing something?
The text was updated successfully, but these errors were encountered:
According to the Phoenix documentation, if no file is selected,
Plug.Upload
will not be present in the params map.So if you submit a blank form,
params[:user]
will benil
. For example:Notice the absence of the "user" key.
Which raises an error in the controller.
We can hack around the error by including a hidden field in the form, forcing
params[:user]
to exist.But
Plug.Upload
will still be missing from the params. Which means the changeset will be valid. Noticechanges
being an empty map.So, my questions are this:
|> validate_required([:avatar])
given thatPlug.Upload
is only present with file data? To reach this point in the changeset, a field must be present. It seems redundant then to validate for presence.avatar
?Here's my user schema:
Am I missing something?
The text was updated successfully, but these errors were encountered: