Skip to Content
Phoenix Web Development
book

Phoenix Web Development

by Brandon Richey
April 2018
Beginner to intermediate content levelBeginner to intermediate
406 pages
9h 33m
English
Packt Publishing
Content preview from Phoenix Web Development

Writing custom validations

In some scenarios, we may have custom error messages and validations that don't really match up to the provided Ecto validations out of the box. We have two ways to handle this with Ecto:

  1. We write a custom validation using Ecto's built-in validate_change function with our own custom anonymous validator function.
  2. We write a totally brand new validation using our own function and some of Ecto's other functions.

Let's explore both options. First, if we want to use validate_change, it's very simple. We'll start with the simplest implementation and then refactor it to get a good sense of how it all fits together:

def changeset(%User{}=user, attrs) do  user  # ...  |> validate_change(:email, fn :email, value -> if value ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Phoenix in Action

Phoenix in Action

Geoffrey Lessel
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781787284197Supplemental Content