Hello Guys,
I was doing the course of the PraProg Phoenix LiveView Pro and I was trying to do a validation for the upload files.
For example If I try to put just the name of the desks and not put any file for upload, I would like to show the validation for upload a file.
I tried to add a validate_required
for the photo_urls
on the changeset:
defmodule LiveViewStudio.Desks.Desk do
use Ecto.Schema
import Ecto.Changeset
schema "desks" do
field :name, :string
field :photo_urls, {:array, :string}, default: []
timestamps()
end
@doc false
def changeset(desk, attrs) do
desk
|> cast(attrs, [:name, :photo_urls])
|> validate_required([:name, :photo_urls])
end
end
But if I put just the name for the desks, the photo_urls are not validated.
as you can see the log:
[debug] QUERY OK db=2.1ms queue=1.3ms idle=1529.6ms
INSERT INTO "desks" ("name","photo_urls","inserted_at","updated_at") VALUES ($1,$2,$3,$4) RETURNING "id" ["Testing", [], ~N[2021-04-19 17:20:41], ~N[2021-04-19 17:20:41]]
But on the application this values are not showed. It shows only the those which have photo_urls.
But If I try to list all Desks, like:
iex> Desks.list_desks()
The empty photo_urls
are recorded:
%LiveViewStudio.Desks.Desk{
__meta__: #Ecto.Schema.Metadata<:loaded, "desks">,
id: 17,
inserted_at: ~N[2021-04-16 21:11:22],
name: "Testing",
photo_urls: [],
updated_at: ~N[2021-04-16 21:11:22]
},
How can I validate an empty array of strings for the :photo_urls
?