Add an error to an Ecto Changeset
If you want to create custom validations with ecto changeset, chances are you are going to need to add errors to the changeset to be shown to the user.
Using add_error/4
allows you to add an error to a changeset.
changeset = change(%BlogPost{}, %{content: "Hello World ...cont"})
add_error(changeset, :content, "Your blog content is too long!")
changeset.errors
[content: {"Your blog content is too long!", []}]
Tweet