Ecto.Changeset.validate_change
You're seeing just the function
validate_change
, go back to Ecto.Changeset module for more information.
Specs
validate_change( t(), atom(), (atom(), term() -> [{atom(), String.t()} | {atom(), {String.t(), Keyword.t()}}]) ) :: t()
Validates the given field
change.
It invokes the validator
function to perform the validation
only if a change for the given field
exists and the change
value is not nil
. The function must return a list of errors
(with an empty list meaning no errors).
In case there's at least one error, the list of errors will be appended to the
:errors
field of the changeset and the :valid?
flag will be set to
false
.
Examples
iex> changeset = change(%Post{}, %{title: "foo"})
iex> changeset = validate_change changeset, :title, fn :title, title ->
...> # Value must not be "foo"!
...> if title == "foo" do
...> [title: "cannot be foo"]
...> else
...> []
...> end
...> end
iex> changeset.errors
[title: {"cannot be foo", []}]
Specs
validate_change( t(), atom(), term(), (atom(), term() -> [{atom(), String.t()} | {atom(), {String.t(), Keyword.t()}}]) ) :: t()
Stores the validation metadata
and validates the given field
change.
Similar to validate_change/3
but stores the validation metadata
into the changeset validators. The validator metadata is often used
as a reflection mechanism, to automatically generate code based on
the available validations.
Examples
iex> changeset = change(%Post{}, %{title: "foo"})
iex> changeset = validate_change changeset, :title, :useless_validator, fn
...> _, _ -> []
...> end
iex> changeset.validations
[title: :useless_validator]