gpt4 book ai didi

elixir - 必填字段不适用于 Phoenix 框架

转载 作者:行者123 更新时间:2023-12-03 18:35:54 25 4
gpt4 key购买 nike

web/models/post.ex

defmodule Baby.Post do
use Baby.Web, :model

schema "posts" do
field :cover, :string
field :email, :string
field :firstname, :string
field :lastname, :string
field :birthday_day, :integer
field :birthday_month, :integer
field :birthday_year, :integer
field :description, :string
field :phone, :string

timestamps
end

@required_fields ~w(email firstname lastname birthday_day birthday_month birthday_year description phone)
@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

web/controllers/post_controller.ex
defmodule Baby.PostController do

use Baby.Web, :controller

alias Baby.Post



def new(conn, _params) do

changeset = Post.changeset(%Post{})
render conn, "new.html", changeset: changeset

end

def create(conn, %{"post" => post_params}) do

changeset = Post.changeset(%Post{}, post_params)

case Repo.insert(changeset) do

{:ok, _post} ->
conn
|> put_flash(:info, "Ton annonce à bien été ajoutée")
|> redirect(to: "/")

{:error, changeset} ->
IO.inspect changeset.valid?
IO.inspect changeset.errors
render(conn, "new.html", changeset: changeset)

end

end
end

web/templates/post/new.html.ex
<%= if @changeset.action do %>
<div class="alert alert-danger">
<p>Oops, something went wrong! Please check the errors below.</p>
</div>
<% end %>

<%= form_for @changeset, post_path(@conn, :create), fn f -> %>

<label for="email">Email</label><br/>
<%= text_input f, :email %><br/><br/>

<label for="email">Prénom</label><br/>
<%= text_input f, :firstname %><br/><br/>
<%= error_tag f, :firstname %>

<label for="email">Nom de famille</label><br/>
<%= text_input f, :lastname %><br/><br/>

<label for="email">Date de naissance</label><br/>
<%= text_input f, :birthday_day, placeholder: "Jour" %>
<%= text_input f, :birthday_month, placeholder: "Mois" %>
<%= text_input f, :birthday_year, placeholder: "Année" %>

<br/><br/>
<%= error_tag f, :birthday_day %>
<%= error_tag f, :birthday_month %>
<%= error_tag f, :birthday_year %>


<label>Description</label><br/>
<%= textarea f, :description %><br/><br/>
<%= error_tag f, :description %>

<label>Numéro de téléphone</label><br/>
<%= text_input f, :phone %><br/><br/>
<%= error_tag f, :phone %>

<button type="submit">Ajouter mon annonce</button>

<% end %>

当我提交带有空输入的表单以创建新帖子时,返回的错误是:
[birthday_day: "is invalid", birthday_month: "is invalid",
birthday_year: "is invalid"]

我也应该有以下错误:电子邮件、名字、姓氏、描述、电话……

最佳答案

那是因为当您提交表单而不填写字段时,会提交一个空字符串。您需要使用 scrub_params 插入 Controller ,用 nils 替换空字段。

将此添加到您的 Controller :

plug :scrub_params, "post" when action in [:create]

这是 Phoenix 在 mix phoenix.gen.html 中默认生成的内容。发电机。 (好吧,它生成 when action in [:create, :update] ;您可能希望在添加 update 操作时切换到该操作。)

您仍然收到 birthday_day 验证错误的原因, birthday_month , 和 birthday_year是因为空字符串不是有效的整数。

关于elixir - 必填字段不适用于 Phoenix 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36821268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com