Why make a dynamic value dependent on actor to be default?
I think you could do something like:
argument :your_argument, :string do
allow_nil? true
default "default_value"
end
# change relate_actor(:user) <- maybe you want this as well
change fn %{data: data, arguments: arguments} = changeset, _context ->
value = case // get actor here // do
... -> ...
nil -> arguments.your_argument
end
Ash.Changeset.force_change_attribute(changeset, :your_attribute, value)
end
You can dbg changeset and context. Make sure to pass the actor in your action call in options.
Not sure if best approach though Perhaps Zach will have a better idea.
We are building some internal used system, and would like to provide dynamic default values for different actors (roles actually) on forms.
For some reason, we got to use generic action, so we got to use the AshPhoenix.Form.for_action(). But when we try to set dynamic default value of a form using Ash.ActionInput.set_argument(input, :arg1, value1) in :prepare_source option, we don’t see any different on the form. That’s why I asked the way to set the dynamic default value in the “default” option.
Fortunately, when we use %Ash.ActionInput{input | arguments: %{arg1: value1}} in :prepare_source option to set dynamic default values, it works! And since we can get actor in the :prepare_source option when calling AshPhoenix.Form.for_action(), this issue is resolved.
I’m just wondering:
Why Ash.ActionInput.set_argument(input, :arg1, value1) in :prepare_source option does not work?
Is %Ash.ActionInput{input | arguments: %{arg1: value1}} in :prepare_source option the right way to do so?
Those should be for all intents and purposes the same thing. I’m not really sure why one would work and one wouldn’t It is the trouble with action_input is that they don’t have a concept of preparations/changes the way that queries do. This is something I’d like to change in the future. What you can do to make things a bit simpler is just set the default in the params when creating the form. Would that work? i.e Form.for_action(..., params: %{"default" => "values"})
Could you print your input? Reasons for the skip might be if input does not have action key and that action has to have arguments in which one of them is the sent one i.e. Enum.find is used.
Hmm…looking at the code, I wasn’t hallucinating. There definitely should be a params option when creating new forms. I tried it myself just recently and it worked
I’m faceing a deadline on some work, so the example to reproduce the error will be created later next week.
If a simple example will work without error, I might have to revisit my code to see the differences, like arguments with default value or so, and apply them to this simple example, so it might need some more works to reproduce the error.
I does indeed work but it’s not mentioned in the docs or rather in @for_options. But what’s weird is the error (Spark.Options.ValidationError) unknown options [:params] because validate_opts_with_extra_keys is used where params ends up in extra keys that are not spark validated and are merged with validated for options. Weird