How to make the function that provids default value of an argument of a generic action be able to get the actor?

Ash.Resource — ash v3.5.2 says that the default value can be a zero arity function e.g. &MyMod.my_fun/0 or a value.

What if the default argument value should depend on the actor? Is there a way to get the actor in the zero arity function?

Please help.

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 :thinking: Perhaps Zach will have a better idea. :bulb:

Thanks @ken-kost for your reply.

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:

  1. Why Ash.ActionInput.set_argument(input, :arg1, value1) in :prepare_source option does not work?
  2. 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 :thinking: 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"})

Thank you for your prompt reply. But the :params option in Form.for_action() get this error msg:

[error] GenServer #PID<0.2885.0> terminating
** (KeyError) key :action not found in: {:error,
 %Ash.Error.Unknown{
   errors: [
     %Ash.Error.Unknown.UnknownError{
       error: "** (Spark.Options.ValidationError) unknown options [:params], valid options are: [:domain, :context, :authorize?, :tenant, :actor, :skip_unknown_inputs, :tracer, :private_arguments]",
       field: nil,
       value: nil,
       splode: Ash.Error,
       bread_crumbs: [],
       vars: [],
       path: [],
       stacktrace: #Splode.Stacktrace<>,
       class: :unknown
     }
   ]
 }}

It looks better if Form.for_action() has this :params option though.

I think this may be a bug? Can you open an issue with examples of your code and stack traces of the error etc. on ash_phoenix

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.

AFAIS for_ calls do not have params option. :thinking:

Oh you’re right, you’d want to validate once with params after calling the action, sorry :slight_smile: my inner LLM hallucinated that one :joy:

1 Like

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 :smiley:

1 Like

Thanks for your kindly help.

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.

Thank you very much!

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 :thinking: