Prepare_params vs. transform_params in ash_phoenix?

* `:prepare_params` - A 2-argument function that receives the params map and the :validate atom and should return prepared params. Called before the form is validated.
* `:transform_params` - A function for post-processing the form parameters before they are used for changeset validation/submission. Use a 3-argument function to pattern match on the [`AshPhoenix.Form`](https://hexdocs.pm/ash_phoenix/AshPhoenix.Form.html) struct. 

prepare_params is called before the "form validation, and transform_params is called before "changeset validation.
But I don’t get what’s different between “form validation” and “changeset validation”.

prepare_params happens just before we handle nested forms, and gets called w/ all top level params, so is the simplest place to modify params.

transform_params is a bit more complex as it allows distinguishing between “is this a nested form” and pattern matching on the form struct who’s params you are modifying.

When in doubt, use prepare_params as a simple callback to modify the params the user submitted before handled by the form.

When is transform_params called with nested form struct?
Could you provide me a simple example of how to use transform_params?

And in ash framework book, I found transform_params is used more.
What’s the reason, even if the prepare_params is simpler?

I was wrong :smiley: transform_params is the preferred approach. Misremembered my own implementation. We’ll update the docs. @sevenseacat reminded me of that.

Sorry for misleading you earlier! Neither one is called for nested forms automatically.

1 Like