arc icon indicating copy to clipboard operation
arc copied to clipboard

How to validate_required in changeset?

Open justuseapen opened this issue 7 years ago • 1 comments

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.

justuseapen avatar Feb 01 '18 17:02 justuseapen

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.

pedep avatar Feb 02 '18 15:02 pedep