为什么生成的表单的验证检查变更集的 `:action` 字段而不是 `:valid?`?

Why does the generated form's validation check the changeset's `:action` field instead of `:valid?`?

例如,发布

mix phx.gen.html Accounts User users name:string age:integer

命令from the docs would generate form.html.eex:

<%%= form_for @changeset, @action, fn f -> %>
  <%%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>Oops, something went wrong! Please check the errors below.</p>
    </div>
  <%% end %>
<%= for {label, input, error} <- inputs, input do %>
  <%= label %>
  <%= input %>
  <%= error %>
<% end %>
  <div>
    <%%= submit "Save" %>
  </div>
<%% end %>

有没有不检查 @changeset.valid? == false 的原因?

当前的表格确实更短,并且在没有错误的情况下,了解 :action 键只是 nil 是有教育意义的。

是否还有其他需要考虑的因素(例如,稍后添加更多字段),或者这只是个人喜好问题?

不知何故错过了 Phoenix.HTML.Form documentation,在“关于 :errors”的部分,它说那

If no action has been applied to the changeset or action was set to :ignore, no errors are shown on the form object even if the changeset has a non-empty :errors value.

This is useful for things like validation hints on form fields, e.g. an empty changeset for a new form. That changeset isn’t valid, but we don’t want to show errors until an actual user action has been performed.

默认情况下空的变更集确实无效:

iex> %Ecto.Changeset{}
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: nil, valid?: false>