How to set a default value for AshPhoenix.Form.for_action ?
I have a Resource that has a foreign key in it which I assign as a hidden field in my form.
How do I make it so that I can fill out the foreign key field when calling
AshPhoenix.Form.for_action
or AshPhoenix.Form.for_create
?3 Replies
There are two main ways to do it.
1. use
prepare_source
coupled with before_submit
to set the belongs_to on the changeset. Looks different depending on if you are setting arguments/attributes, but here is an example
The before_submit
is important to make sure that someone doesn't mess with the underlying form HTML to set foo_id
after our prepare_source
runs. The flow there is prepare_source -> pass user input to action -> before_submit
. You can also use private arguments for this purpose, which means the value won't be accepted from user input, only with explicit calls to Ash.Changeset.set_argument
That would avoid the need for the before_submit
hook in addition to the rest.
2. Put the action on the parent thing. For example:
Then
And in your HTML
If you do that, Ash will take care of relating the child with the parent, because thats what manage_relationship
does.This is so detailed. Thanks for helping!
My pleasure 😄