Ash.Changeset.add_error() for a nested form argument

I have a nested form based on a resource, Task, that has_many :keydates. The nested keydate_form has an argument as a virtual field whose validation error (via add_error) is not being displayed nor does it stop the form submission. error_path was added to the change manage_relationship statement as suggested by the Ash documentation, but it doesn’t seem to help. What am I missing?

Form in mount()

    form =
      AshPhoenix.Form.for_update(task, :update,
        forms: [
          auto?: true
      ]

Heex

          <.input
            field={{@keydate_form, :argument1}}
            type="text"
            label=""
          />

Task resource


  actions do
   update :update do
      ...
     argument :keydates, {:array, :map}
     change manage_relationship(:keydates, type: :direct_control, **error_path: :keydates**)
    end

Keydate resource


  actions do
    update :update do
         change fn changeset, %{actor: actor} ->
        Ash.Changeset.before_action(changeset, fn changeset ->

          Ash.Changeset.add_error(changeset, [field: :argument1, message: "Input not valid."])
        end)
      end

Changeset in Form submission error (excerpt)

...,
arguments: %{
    keydates: [
       %{
         "argument1" => "123",
          ...
        }
]}
    errors: [
      %Ash.Error.Changes.InvalidAttribute{
        field: :argument1,
        message: "Input not valid.",
        private_vars: nil,
        value: nil,
        changeset: nil,
        query: nil,
        error_context: [],
        vars: [],
        path: [],
        stacktrace: #Stacktrace<>,
        class: :invalid
      }
    ],

Question, in your logs, do you see a rollback happening? There was a bug recently where errors that happened that caused a rollback weren’t getting path added to them. I don’t recall if this was ported back to 2.0 or not yet.

@zachdaniel Yes, the log shows rollback [] right after the add_error step.

Got it. If you don’t mind, please open an issue on ash requesting a back port of the error handling fix around embeds and transactions. I’ll know what it means, and will address it as soon as I can.

1 Like