View Source ChangesetHelpers (Changeset Helpers v0.23.0)
Provides a set of helpers to work with Changesets.
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.
Works like Ecto.Changeset.validate_change/3 but may receive multiple fields.
Validates the result of the comparison of
Validates a list of values using the given validator.
validate_not_changed/3 checks if the specified attribute is present in the changeset's parameters.
If the attribute is present, an error is added to the changeset's :errors key.
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).
Works like Ecto.Changeset.validate_change/3 but may receive multiple fields.
The validator function receives as argument a keyword list, where the keys are the field
names and the values are the change for this field, or the data.any()
If one of the fields is nil, the validator function is not invoked.
validate_comparison(changeset, field1, operator, field2_or_value, opts \\ [])
View SourceValidates the result of the comparison of
- two fields (where at least one is a change) or
- a change and a value, where the value is an integer, a
Date, aTime, aDateTimeor aNaiveDateTime.
Options
:error_on_field- specifies on which field to add the error, defaults to the first field:message- a customized message on failure
validate_list(changeset, field, validation_fun, validation_fun_args)
View SourceValidates a list of values using the given validator.
changeset =
%Appointment{}
|> Appointment.changeset(%{days_of_week: [1, 3, 8]})
|> validate_list(:days_of_week, &Ecto.Changeset.validate_inclusion/3, [1..7])
assert [days_of_week: {"is invalid", [validation: :list, index: 2, validator: :validate_inclusion]}] = changeset.errors
assert [days_of_week: {:list, [validator: :validate_inclusion]}] = changeset.validations
validate_not_changed/3 checks if the specified attribute is present in the changeset's parameters.
If the attribute is present, an error is added to the changeset's :errors key.