Ash FrameworkAF
Ash Framework3mo ago
11 replies
Rutgerdj

`update_form` doesn't update params on root form

Im using the AshPhoenix.Form.update_form function to update a subform of the root form.

If after the update_form I call a validate or update_params on the root form the params are reset to the values before the update_form call.
Since a code snippet says more than 1000 words:


    # Create form and add author sub-form with default value for email
    form =
      AshPhoenix.Form.for_create(Post, :create, forms: [auto?: true])
      |> AshPhoenix.Form.add_form(:author,
        params: %{
          "email" => "example@email.org"
        }
      )

    # Use update form to update the email of the author
    form =
      AshPhoenix.Form.update_form(form, :author, fn author_form ->
        AshPhoenix.Form.update_params(author_form, fn params ->
          Map.put(params, "email", "changed@email.org")
        end)
      end)

    assert AshPhoenix.Form.get_form(form, [:author]) |> AshPhoenix.Form.value(:email) == "changed@email.org"

    # Now update some other attributes on the root form
    form =
      AshPhoenix.Form.update_params(form, fn params ->
        Map.put(params, "text", "text value")
      end, only_touched?: true, target: ["text"])

    # This fails
    assert AshPhoenix.Form.get_form(form, [:author]) |> AshPhoenix.Form.value(:email) == "changed@email.org"


I'm wondering whether this is a bug or if there's something I'm missing in the usage of update_form.
Was this page helpful?