October 2019
Intermediate to advanced
358 pages
8h 22m
English
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 ...
Read now
Unlock full access