我可能会遗漏一些东西,但感觉这应该是无效的记录:
pry(1)> project_params
%{"name" => ""}
pry(2)> changeset
%Ecto.Changeset{action: nil, changes: %{name: ""}, constraints: [], errors: [],
filters: %{},
model: %Elix.Project{__meta__: #Ecto.Schema.Metadata<:built>, id: nil,
inserted_at: nil, name: nil, updated_at: nil,
user: #Ecto.Association.NotLoaded<association :user is not loaded>,
user_id: 2}, optional: [], opts: [], params: %{"name" => ""}, prepare: [],
repo: nil, required: [:name, :user_id],
types: %{id: :id, inserted_at: Ecto.DateTime, name: :string,
updated_at: Ecto.DateTime, user_id: :id}, valid?: true, validations: []}
pry(3)> changeset.valid?
true
以下是我定义模型的方法:
项目
defmodule Elix.Project do
use Elix.Web, :model
schema "projects" do
field :name, :string
belongs_to :user, Elix.User
timestamps
end
@required_fields ~w(name user_id)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
用户
defmodule Elix.User do
use Elix.Web, :model
schema "users" do
field :email, :string
field :password, :string, virtual: true
field :crypted_password, :string
has_many :projects, Elix.Project
timestamps
end
@required_fields ~w(email password)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> unique_constraint(:email)
|> validate_format(:email, ~r/@/)
|> validate_length(:password, min: 5)
end
end
这是控制器创建操作的代码:
def create(conn, %{"project" => project_params}) do
changeset =
Ecto.build_assoc(conn.assigns.current_user, :projects) |>
Project.changeset(project_params)
IEx.pry
case Repo.insert(changeset) do
{:ok, project} ->
conn
|> put_flash(:info, "Project #{project.name} created succesfully")
|> redirect(to: project_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", project: changeset)
end
end
我提交表单时没有故意输入任何内容,因此我可能会测试表单显示错误。我在这里缺少什么?
答案 0 :(得分:2)
如果changeset.valid?
的值为false
,name
将仅为nil
。由于您的案例中name
的值为空字符串,因此更改集将有效。
通过添加scrub_params
插件来处理将空字符串作为值传递给控制器的表单的最佳位置:
plug :scrub_params, "project" when action in [:create, :update]
查看documentation以获取有关scrub_params
。