使用 Phoenix.HTML.Form "With connection data" 添加并显示错误消息
Add and show an error message with Phoenix.HTML.Form "With connection data"
我有一个带有文本区域的简单表单,用户可以在其中输入(或复制粘贴)一个 json 字符串。表单将发送到我的控制器操作,我将验证 json 字符串,如果没问题,我将创建一些记录,一切都很好。
为了将表单发送到我的控制器操作,我现在使用 Phoenix.HTML.Form "With connection data",因此我没有 model/changeset。
<%= form_for @conn, @action, [as: :match], fn f -> %>
<%= textarea f, :json, rows: 20 %>
<%#= error_tag @changeset, f, :json %>
<% end %>
如果 json 由于某种原因无效,我想再次呈现表单并显示错误消息。 error_tag
是一种视图辅助方法,如果有变更集,它将在字段中显示错误。因此,它现在被注释掉了。
def error_tag(form, field) do
if error = form.errors[field] do
content_tag :span, (humanize(field) <> " " <> translate_error(error)), class: "help-block"
end
end
def error_tag(changeset, form, field) do
if changeset.action do
error_tag(form, field)
end
end
添加错误以便我可以在表单字段中显示它们的正确方法是什么。我是否必须将错误添加到 @conn
或 f
(表单)或是否有其他方法?
在你的控制器模块中
创建包含操作中错误的关键字列表
errors = [field: "error message"]
然后在调用render方法时传递
render(conn, "template.html", errors: errors)
在你的视图模块中
添加辅助函数以从关键字列表中提取和格式化消息
def error_tag(errors, field) when is_list(errors) and is_atom(field) do
case Keyword.fetch(errors, field) do
{:ok, message} -> content_tag :span, (humanize(field) <> " " <> translate_error(message)), class: "help-block"
:error -> html_escape("")
end
end
在您的模板文件中
显示错误信息
<%= form_for @conn, @action, [as: :match], fn f -> %>
<%= textarea f, :json, rows: 20 %>
<%= error_tag @errors, :json %>
<% end %>
我决定实现一个验证 json 字符串的模块。我向其中添加了自定义错误的模块方法 changeset
returns 和 Ecto.Changeset
。私有 validate_json
方法检查 json 架构和内容。毕竟我将更改和参数设置为表单中给定的参数并且我设置有效?我是否有错误。
def changeset(required_fields, json_field, params) do
changeset =
%Ecto.Changeset{model: %MyApp.ModelName{}}
|> Ecto.Changeset.cast(params, required_fields, ~w())
|> validate_json(json_field, params)
%Ecto.Changeset{changeset | changes: params, params: params, valid?: Enum.count(changeset.errors) == 0}
end
通过这种方式,我可以保持我的视图和部分不变,并且表单和所有辅助方法都像以前一样与我的变更集一起工作。
另一种方法是创建一个 embeded_schema
,您可以在其中定义包括验证在内的所有数据模型,而无需持久化到数据库中,并依赖 Phoenix 中提供的 validation/error 管理。 José Valim explains 如何使用这种方法。
您只需以这种方式创建架构:
data = %{}
types = %{first_name: :string, last_name: :string, email: :string}
changeset =
{data, types} # The data+types tuple is equivalent to %Registration{}
|> Ecto.Changeset.cast(params["sign_up"], Map.keys(types))
|> validate_required(...)
|> validate_length(...)
然后照常显示错误:
<%= error_tag f, :first_name %>
我有一个带有文本区域的简单表单,用户可以在其中输入(或复制粘贴)一个 json 字符串。表单将发送到我的控制器操作,我将验证 json 字符串,如果没问题,我将创建一些记录,一切都很好。
为了将表单发送到我的控制器操作,我现在使用 Phoenix.HTML.Form "With connection data",因此我没有 model/changeset。
<%= form_for @conn, @action, [as: :match], fn f -> %>
<%= textarea f, :json, rows: 20 %>
<%#= error_tag @changeset, f, :json %>
<% end %>
如果 json 由于某种原因无效,我想再次呈现表单并显示错误消息。 error_tag
是一种视图辅助方法,如果有变更集,它将在字段中显示错误。因此,它现在被注释掉了。
def error_tag(form, field) do
if error = form.errors[field] do
content_tag :span, (humanize(field) <> " " <> translate_error(error)), class: "help-block"
end
end
def error_tag(changeset, form, field) do
if changeset.action do
error_tag(form, field)
end
end
添加错误以便我可以在表单字段中显示它们的正确方法是什么。我是否必须将错误添加到 @conn
或 f
(表单)或是否有其他方法?
在你的控制器模块中
创建包含操作中错误的关键字列表
errors = [field: "error message"]
然后在调用render方法时传递
render(conn, "template.html", errors: errors)
在你的视图模块中
添加辅助函数以从关键字列表中提取和格式化消息
def error_tag(errors, field) when is_list(errors) and is_atom(field) do
case Keyword.fetch(errors, field) do
{:ok, message} -> content_tag :span, (humanize(field) <> " " <> translate_error(message)), class: "help-block"
:error -> html_escape("")
end
end
在您的模板文件中
显示错误信息
<%= form_for @conn, @action, [as: :match], fn f -> %>
<%= textarea f, :json, rows: 20 %>
<%= error_tag @errors, :json %>
<% end %>
我决定实现一个验证 json 字符串的模块。我向其中添加了自定义错误的模块方法 changeset
returns 和 Ecto.Changeset
。私有 validate_json
方法检查 json 架构和内容。毕竟我将更改和参数设置为表单中给定的参数并且我设置有效?我是否有错误。
def changeset(required_fields, json_field, params) do
changeset =
%Ecto.Changeset{model: %MyApp.ModelName{}}
|> Ecto.Changeset.cast(params, required_fields, ~w())
|> validate_json(json_field, params)
%Ecto.Changeset{changeset | changes: params, params: params, valid?: Enum.count(changeset.errors) == 0}
end
通过这种方式,我可以保持我的视图和部分不变,并且表单和所有辅助方法都像以前一样与我的变更集一起工作。
另一种方法是创建一个 embeded_schema
,您可以在其中定义包括验证在内的所有数据模型,而无需持久化到数据库中,并依赖 Phoenix 中提供的 validation/error 管理。 José Valim explains 如何使用这种方法。
您只需以这种方式创建架构:
data = %{}
types = %{first_name: :string, last_name: :string, email: :string}
changeset =
{data, types} # The data+types tuple is equivalent to %Registration{}
|> Ecto.Changeset.cast(params["sign_up"], Map.keys(types))
|> validate_required(...)
|> validate_length(...)
然后照常显示错误:
<%= error_tag f, :first_name %>