ChangesetHelpers (Changeset Helpers v0.10.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

Link to this function

add_error(changeset, keys, message, extra \\ [])

View Source

Adds an error to the nested changeset.

account_changeset =
  ChangesetHelpers.add_error(account_changeset, [:user, :articles, :error_key], "Some error")
Link to this function

change_assoc(struct_or_changeset, keys)

View Source
Link to this function

change_assoc(struct_or_changeset, keys, changes)

View Source

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"})
Link to this function

change_assoc(struct_or_changeset, keys, index, changes)

View Source

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).

Link to this function

diff_field(changeset1, changeset2, keys)

View Source

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])
Link to this function

do_raise_if_invalid_fields(changeset, keys_validations)

View Source
Link to this function

fetch_change(changeset, list)

View Source

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])
Link to this function

fetch_change!(changeset, keys)

View Source

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])
Link to this function

fetch_field(changeset, list)

View Source

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])
Link to this function

fetch_field!(changeset, keys)

View Source

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])
Link to this function

put_assoc(changeset, keys, value)

View Source

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).

Link to this function

put_assoc(changeset, keys, index, value)

View Source

Puts the given nested association in the changeset at the given index.

See put_assoc(changeset, keys, value).

Link to this function

raise_if_invalid_fields(changeset, keys_validations)

View Source

Raises if one of the given field has an invalid value.