Ash.Changeset.for_create
for_create
, go back to Ash.Changeset module for more information.
Constructs a changeset for a given create action, and validates it.
Anything that is modified prior to for_create/4
is validated against the rules of the action, while anything after it is not.
This runs any change
s contained on your action. To have your logic execute only during the action, you can use after_action/2
or before_action/2
.
Multitenancy is not validated until an action is called. This allows you to avoid specifying a tenant until just before calling the api action.
Params
params
may be attributes, relationships, or arguments. You can safely pass user/form input directly into this function.
Only public attributes and relationships are supported. If you want to change private attributes as well, see the
Customization section below. params
are stored directly as given in the params
field of the changeset, which is used
Opts
:relationships
- customize relationship behavior.
By default, any relationships are ignored. There are three ways to change relationships with this function:Action Arguments (preferred)
Create an argument on the action and add a
Ash.Resource.Change.Builtins.manage_relationship/3
change to the action.Overrides
You can pass the
relationships
option to specify the behavior. It is a keyword list of relationship and eitherone of the preset manage types: [:replace, :append, :remove, :direct_control, :create]
explicit options, in the form of
{:manage, [...opts]}
Ash.Changeset.for_create(MyResource, :create, params, relationships: [relationship: :append, other_relationship: {:manage, [...opts]}])
You can also use explicit calls to
manage_relationship/4
.:require?
- If set totrue
, values are only required when the action is run (instead of immediately). The default value isfalse
.:actor
- set the actor, which can be used in anyAsh.Resource.Change
s configured on the action. (in thecontext
argument):tenant
- set the tenant on the changeset
Customization
A changeset can be provided as the first argument, instead of a resource, to allow setting specific attributes ahead of time.
For example:
MyResource
|> Ash.Changeset.new()
|> Ash.Changeset.change_attribute(:foo, 1)
|> Ash.Changeset.for_create(:create, ...opts)
Once a changeset has been validated by for_create/4
(or for_update/4
), it isn't validated again in the action.
New changes added are validated individually, though. This allows you to create a changeset according
to a given action, and then add custom changes if necessary.