Validation in a multistep registration form

I have a user resource which gets populated with data in a multilevel registration form. First the human has to enter email, username and password which will be saved in the database. In the next step/form his/her birthdate. In the third step/form his/her zip_code. Of course I want to validate those information but I have no idea how to do that best since the whole resource will only be completed in the very last step. But I want to validate the steps before too.

What is a good approach for this?

defmodule Animina.Accounts.User do
  @moduledoc """
  This is the User module.
  """

  use Ash.Resource,
    data_layer: AshPostgres.DataLayer,
    extensions: [AshAuthentication]

  attributes do
    uuid_primary_key :id
    attribute :email, :ci_string, allow_nil?: false
    attribute :hashed_password, :string, allow_nil?: false, sensitive?: true
    attribute :username, :string, allow_nil?: false
    attribute :birthday, :date, allow_nil?: false
    attribute :zip_code, :string, allow_nil?: false
  end
  # ...
1 Like

So, there are a lot of ways that you could potentially tackle this, TBH. What many folks will do is create embedded resources for the various inputs in each step of the form. For that, AshPhoenix.Form.submit will just return the validated structs, or a form if there are errors. Then, when all the forms have been submitted, you gather up all the values and submit the actual resource action.

2 Likes

That is my idea too. I wanted to be sure if that is a good way because it involves duplicate code in both resources. Thanks!