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(user, attrs) do |
| user |
| |> cast(attrs, [:name, :username]) |
| |> validate_required([:name, :username]) |
| |> validate_length(:username, min: 1, max: 20) |
| end |
The Ecto.Changeset.cast function converts a raw map of user input to a changeset, accepting only the :name and :username keys. Then, we fire a validation limiting the length of valid usernames to twenty characters. A failing validation places errors in the changeset so we can display them to the user.
As you might expect, you’ll use one changeset per ...
Get Programming Phoenix 1.4 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.