‹ Elliot Jackson

Tags / Ecto

Written for Phoenix 1.5.7 & Ecto 3.5.5 Ecto changesets provide common validation options but we can also write our own. Before we get into writing our own though, we need to understand how (most of) the default validators work. Let’s have a look at the definition of validate_inclusion for example: def validate_inclusion(changeset, field, data, opts \\ []) do validate_change changeset, field, {:inclusion, data}, fn _, value -> if value in data, do: [], else: [{field, {message(opts, "is invalid"), [validation: :inclusion, enum: data]}}] end end We can see that the actual logic is wrapped inside validate_change/4.
« Older posts Newer posts »