I'm attempting to insert a list into a field with type {:array, :string}
but keep receiving a "is invalid" error once the form is submitted. Here's my relevant code:
Migration
def change do
alter table(:articles) do
add :images, {:array, :string}
end
end
Model
schema "articles" do
...
field :images, {:array, :string}
...
end
@required_fields ~w(title content user_id)
@optional_fields ~w(images)
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> validate_length(:title, max: 78)
|> strip_unsafe_content(params)
end
...
Controller
def create(conn, %{"article" => %{"images" => nil} = article_params}) do
changeset = Article.changeset(%Article{}, article_params)
case Repo.insert(changeset) do
{:ok, article} ->
conn
|> put_flash(:info, "Article created successfully.")
|> redirect(to: article_path(conn, :show, article))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def create(conn, %{"article" => %{"images" => images} = article_params}) do
images = String.split(images, ",", trim: true)
changeset = Article.changeset(%Article{}, article_params)
|> Ecto.Changeset.change(images: images)
case Repo.insert(changeset) do
{:ok, article} ->
conn
|> put_flash(:info, "Article created successfully.")
|> redirect(to: article_path(conn, :show, article))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
Changeset
%Ecto.Changeset{action: :insert,
changes: %{content: "\tTest",
images: ["images/articles/hh_2016/1.jpg", "images/articles/hh_2016/2.jpg",
"images/articles/hh_2016/3.jpg", "images/articles/hh_2016/4.jpg",
"images/articles/hh_2016/5.jpg", "images/articles/hh_2016/6.jpg",
"images/articles/hh_2016/7.jpg", "images/articles/hh_2016/8.jpg"],
title: "Test", user_id: 31},
constraints: [],
errors: [images: "is invalid"],
...
}
I'm guessing that I've missed something basic but can't figure it out. Any help is appreciated! Let me know if you need more code.
The problem is that you're passing a string as images
to Article.changeset/2
first, which marks images
as invalid, and then putting a valid value later with Ecto.Changeset.change/2
, but Ecto.Changeset.change/2
does not rerun the validations automatically. For your example, you can simply pass the correct images
value to Article.changeset/2
:
images = String.split(images, ",", trim: true)
changeset = Article.changeset(%Article{}, %{article_params | "images" => images)