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
ZachDaniel
ZachDaniel2y ago
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 it
Robert Graff
Robert GraffOP2y ago
How can I set the params before I render the form?
ZachDaniel
ZachDaniel2y ago
you could validate it once w/ your own params
Robert Graff
Robert GraffOP2y ago
Just saw prepare_source, trying to figure out how that works.
ZachDaniel
ZachDaniel2y ago
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
change fn changeset, _ ->
if changeset.arguments[:foo] do
changeset
else
Ash.Changset.set_argument(changeset, :foo, changeset.data.bar)
end
end
change fn changeset, _ ->
if changeset.arguments[:foo] do
changeset
else
Ash.Changset.set_argument(changeset, :foo, changeset.data.bar)
end
end
(for example) changes run when we validate the form (which is why anything w/ side effects goes in before/after action hooks)
Robert Graff
Robert GraffOP2y ago
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.
AshPhoenix.Form.for_update(plan, :update,
api: Environments,
prepare_source: fn changeset ->
Ash.Changeset.set_argument(
changeset,
:feature_keys,
default_feature_keys(changeset.data)
)
end
)
AshPhoenix.Form.for_update(plan, :update,
api: Environments,
prepare_source: fn changeset ->
Ash.Changeset.set_argument(
changeset,
:feature_keys,
default_feature_keys(changeset.data)
)
end
)
I like this a little better because the defaults are more related to the form than the action.
Robert Graff
Robert GraffOP2y ago
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...

Did you find this page helpful?