Closed nelsonic closed 7 years ago
@nelsonic I may be totally off the mark, but what happens if you change:
|> validate_required(params, @required_fields)
to
|> validate_required(@required_fields)
I tried something similar to that and it seemed to work (I'm not as far through the book as you so I can't run the example you are running just yet).
For anyone following "Programming Pheonix" around page 60:
@des-des and I were running into the same warnings and ended up doing the below
def changeset(model, params \\ :invalid) do
model
|> cast(params, ~w(name username))
|> validate_required([:name, :username])
|> validate_length(:username, min: 1, max: 20)
end
params
is injected as the first argument to validate_required
I would add to that: the book adds a uniqueness index to the username
field, but doesn't suggest a way to validate that uniqueness:
def changeset(model, params \\ :invalid) do
model
|> cast(params, ~w(name username))
|> validate_required([:name, :username])
|> unique_constraint(:username)
|> validate_length(:username, min: 1, max: 20)
end
I would recommend also adding the |> unique_constraint(:username)
to the username validations
@untra thanks for sharing. I have a slight variation on this:
def changeset(model, params \\ :invalid) do
model
|> cast(params, ~w(name username))
|> validate_required([:name, :username])
|> validate_length(:username, min: 1, max: 20)
|> unique_constraint(:username) # save the unique_constraint for last as it "hits" the DB. ;-)
end
running
mix test
on page 134 but get the following warning message:Looked at: https://hexdocs.pm/ecto/Ecto.Changeset.html
tried changing the code in
web/models/video.ex:20-25
from:To:
or:
but tests failed:
figured I need to call
cast
on all the fields as a single list. read http://learningwithjb.com/posts/concat-lists-with-elixir So changed to:and ran:
mix test
and got:I would like to
continue
spending time on this but I suspect that it will become clear to me later in the book or through more practice/understanding so I'm just going to leave this issue open incase
someoneelse
knows how to "fix" it ...