Ecto Changeset validate_confirmation function is undefined

This related to my other question, but I’m asking a new one for clarity.

Using the built-in validation_confirmation I’m getting:
undefined function validate_confirmation/4.

It’s an Ecto(?) function, so I’m stumped on this one. None of the other functions there fail, only validation_confirmation. Am I missing an import somewhere? Undefined error makes no sense here.


defp validate_email(changeset) do
    IO.inspect(changeset)
    changeset
    |> validate_required([:email])
    |> validate_format(:email, ~r/^[^\s]+@[^\s]+$/, message: "must have the @ sign and no spaces")
    |> validate_length(:email, max: 160)
    |> unsafe_validate_unique(:email, TurnStile.Repo)
    |> unique_constraint(:email)
    |> validate_confirmation(changeset, :password, message: "does not match password")
  end

SOLVED: So the problem here was the param changeset. Since it is being piped it does not take changeset and should be:

|> validate_confirmation(:email, message: "Emails do not match")

Quite a misleading error message, and quite hard to debug.

It does take the changeset as a first parameter, it’s just that the pipe removes the need to type it out.

Example:

def sum(a, b) do
  a + b
end

# the two lines below do the same thing:
sum(1, 2)
1 |> sum(2)
1 Like
defp validate_email(changeset) do
    IO.inspect(changeset)
    changeset
    |> ...
    |> validate_confirmation(changeset, :password, message: "does not match password")
  end

What you had originally would be equivalent to:
validate_confirmation(changeset, changeset, :password, message: "...")
which would explain why the error was undefined function validate_confirmation/4.

1 Like