Skip to Main Content
Programming Phoenix
book

Programming Phoenix

by Chris McCord, Bruce Tate, Jose Valim
April 2016
Beginner to intermediate content levelBeginner to intermediate
300 pages
6h 58m
English
Pragmatic Bookshelf
Content preview from Programming Phoenix

Managing Registration Changesets

You’ve already seen a changeset for creating a new user—the one that handles the name and username. Let’s review that now:

 def​ changeset(model, params \\ ​:empty​) ​do
  model
  |> cast(params, ​~​w(name username), [])
  |> validate_length(​:username​, ​min:​ 1, ​max:​ 20)
 end

The Ecto.Changeset.cast function converts that naked map to a changeset and, for security purposes, limits the inbound parameters to the ones you specify. Then, we fire a validation limiting the length of valid usernames to one to twenty characters. Remember, we need to supply :empty parameters instead of an empty map so Ecto can distinguish our blank new changeset from an empty form submission ...

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

Programming Elm

Programming Elm

Jeremy Fairbank
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781680501926Errata Page