Setting defaults for a form input that is an argument and not an attribute?
I have an update action that takes an argument. In the form for this action, I need to set the default value for the form input. The default value is dependent on the current state of the resource being updated.
I explored using the
default
option for an action argument; however, It accepts a zero-argument function, so there's no way to get the resource being updated.
I explored using the prepare_params
option for AshPhoenix.Form.for_update
but I couldn't find any examples. It's arrity 2 and receives the params but is only called when the form is validated or submitted.
Side quest(ion): what's the difference between prepare_params and transform_params if they're both called only on validate and submit?7 Replies
IIRC
prepare_params
is pre-validate
and transform_params
is post validate
oh, no thats wrong
transform_params
is passed to child forms
so all params on all forms go through itHow can I set the params before I render the form?
you could validate it once w/ your own params
Just saw prepare_source, trying to figure out how that works.
so, lets revisit the initial use case though
if you have a default value that is based on the current state of things, you can do that with a change on the action as well
(for example)
changes run when we validate the form (which is why anything w/ side effects goes in before/after action hooks)
ok, so I can create my input form and then immediately validate it to get the argument set?
I got it working with
prepare_source
.
I like this a little better because the defaults are more related to the form than the action.Made a few doc changes. Hopefully, my understanding is correct. https://github.com/ash-project/ash_phoenix/pull/97/files
GitHub
Improve form documentation by rgraff · Pull Request #97 · ash-proje...
Improvements to the Form documentation based on my understanding and looking at source code. These very small tweaks would've helped me today.
Contributor checklist
Bug fixes include regr...