arc
arc copied to clipboard
How to validate_required in changeset?
We have a few forms in our app with an image upload requirement. We can successfully validate that there is or is not a file upload with the allowed extensions. The problem is we're not sure how to make error messages appear on the form if the user submits without a file.
This must be a solved problem but I can't find anything online that's helped.
Have you tried using validate_required on the field after the cast_attachments?
I am doing this in a project, with a changeset that looks like this ( specifics have been snipped out, but the general idea should work )
def changeset(%Artwork{} = artwork, attrs) do
artwork
|> cast(attrs, [:title])
# |> snip..
|> cast_attachments(attrs, [:file])
|> validate_required([:file, :title])
# |> snip..
end
This should attach an error on your changeset, which can then be handled later on.