Ash FrameworkAF
Ash Framework8mo ago
9 replies
Jhonathas

How to handle submit-only actions without data layer but with validations?

Hi everyone, I'm just getting started with Ash and ran into a challenge when trying to build a LiveView form that validates input and then sends the data to an external API, without using a data_layer.

I'm using a create action with validates and a change/3 callback for the external logic. The issue is: change/3 is called before validations are applied, and changeset.valid? is already true, so my logic executes even when the input is invalid.

Since I'm not using a data_layer, before_action and after_action hooks are not triggered — which would have been ideal for running the API call only after validation.

I already solved the issue of distinguishing between validate and submit (in LiveView) by passing a form_action argument manually.

Now my question is: how can I run logic after validations have been applied, in a non-persistent action?


Here’s a simplified version of what I’m doing:

In the resource

actions do
  create :register do
    argument :form_action, :atom, constraints: [one_of: [:validate, :submit]]
    accept [:name, :document, :mobile, :email]
    change App.Reverse.PatientLogic
  end
end


In the LiveView

case Form.submit(socket.assigns.form, params: Map.put(params, "form_action", :submit)) do
  {:ok, _patient} -> ...
  {:error, form} -> ...
end


In PatientLogic

def change(changeset, _opts, _ctx) do
  if changeset.arguments[:form_action] == :submit and changeset.valid? do
    # Call external API...
  end

  changeset
end


The issue is that changeset.valid? is already true before validations have actually run.

Thanks, folks — really appreciate it in advance!
Was this page helpful?