How to add form errors not enforced by the resource?

So, basically I have a resource that has an attribute that allows nil values, but I want to have a AshPhoenix.Form that will consider that attribute as if it can’t be nil.

In Ecto I would probably just create a different changeset function that will enforce that attribute as required.

How can I do the same with Ash?

You can create a separate action, and give it an argument with the same name, that can’t be nil.

Then use

change set_attribute(:attribute, arg(:argument))

To set it.

1 Like

Hmm, this works, but if I use it in an update form, then the field input will be empty even if the attribute has a value already.

:thinking: you can try adding a change on the action, like:

change fn changeset, _ -> 
  case Ash.Changeset.fetch_argument(changeset, :argument_name) do
    {:ok, _} -> changeset
    :error -> Ash.Changeset.set_argument(changeset, changeset.data.argument_name)
  end
end

Or when creating the form, you can validate once w/ custom params Form.for_update(...) |> Form.validate(%{some_argument: record.some_argument})

Wouldn’t this work in the action?:

validate attribute_does_not_equal(:the_attribute, nil)

Ah, yeah true. Just validate present(:attr)