ChangesetHelpers (Changeset Helpers v0.13.0) View Source
Provides a set of helpers to work with Changesets.
Link to this section Summary
Functions
Adds an error to the nested changeset.
Returns the nested association in a changeset. This function will first look into the changes and then fails back on data wrapped in a changeset.
Returns the nested association in a changeset at the given index.
This function allows checking if a given field is different between two changesets.
Fetches a nested change from the given changeset.
Same as fetch_change/2
but returns the value or raises if the given nested key was not found.
Fetches the given nested field from changes or from the data.
Same as fetch_field/2
but returns the value or raises if the given nested key was not found.
Puts the given nested association in the changeset through a given list of field names.
Puts the given nested association in the changeset at the given index.
Raises if one of the given field has an invalid value.
Link to this section Functions
Adds an error to the nested changeset.
account_changeset =
ChangesetHelpers.add_error(account_changeset, [:user, :articles, :error_key], "Some error")
Returns the nested association in a changeset. This function will first look into the changes and then fails back on data wrapped in a changeset.
Changes may be added to the given changeset through the third argument.
A tuple is returned containing the root changeset, and the changeset of the association.
{account_changeset, address_changeset} =
change_assoc(account_changeset, [:user, :config, :address], %{street: "Foo street"})
Returns the nested association in a changeset at the given index.
A tuple is returned containing the root changeset, the changesets of the association and the changeset at the specified index.
See change_assoc(struct_or_changeset, keys, changes)
.
This function allows checking if a given field is different between two changesets.
{street_changed, street1, street2} =
diff_field(account_changeset, new_account_changeset, [:user, :config, :address, :street])
Fetches a nested change from the given changeset.
This function only looks at the :changes
field of the given changeset
and returns {:ok, value}
if the change is
present or :error
if it's not.
{:ok, street} =
ChangesetHelpers.fetch_change(account_changeset, [:user, :config, :address, :street])
Same as fetch_change/2
but returns the value or raises if the given nested key was not found.
street = ChangesetHelpers.fetch_change!(account_changeset, [:user, :config, :address, :street])
Fetches the given nested field from changes or from the data.
While fetch_change/2
only looks at the current changes to retrieve a value, this function looks at the changes and
then falls back on the data, finally returning :error
if no value is available.
For relations, these functions will return the changeset original data with changes applied. To retrieve raw
changesets, please use fetch_change/2
.
{:changes, street} =
ChangesetHelpers.fetch_field(account_changeset, [:user, :config, :address, :street])
Same as fetch_field/2
but returns the value or raises if the given nested key was not found.
street = ChangesetHelpers.fetch_field!(account_changeset, [:user, :config, :address, :street])
Puts the given nested association in the changeset through a given list of field names.
ChangesetHelpers.put_assoc(account_changeset, [:user, :config, :address], address_changeset)
Instead of giving a Changeset or a schema as the third argument, a function may also be given receiving the nested Changeset(s) to be updated as argument.
ChangesetHelpers.put_assoc(account_changeset, [:user, :articles],
&(Enum.concat(&1, [%Article{} |> Ecto.Changeset.change()])))
In the code above, we add a new empty Article to the articles association (typically done when we want to add a new article to a form).
Puts the given nested association in the changeset at the given index.
See put_assoc(changeset, keys, value)
.
Raises if one of the given field has an invalid value.