Here’s a part of my web/models/user.ex
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:name_user, :first_name, :name_last, :email, :password_hashed])
|> validate_required([:name_user, :first_name, :name_last, :email, :password_hashed])
|> validate_format([:name_user, ~w/], [:first_name, ~w/], [:name_last, ~w/], [:email, ~r/@/], [:password_hashed])
|> validate_length([:name_user, min: 2, max: 254], [:first_name, min: 2, max: 127], [:name_last, min: 2, max: 127], [:email, max: 254], [:password_hashed, min: 8])
|> unique_constraint([:name_user, :email])
end
end
The error I’m getting after doing mix ecto.migrate
is
== Compilation error on file web/models/user.ex ==
** (SyntaxError) web/models/user.ex:22: syntax error before: '@'
(elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
but I’m guessing this isn’t because I’ve not escaped that regex (as I found it on several tutorials where it’s supposedly working) - but rather that I’m writing out the multiple fields for validate_format wrongly.
At first, I wondered if I should use tuples instead. I can’t find examples of how to validate multiple fields.
Do you do this or put each validate_format, or example, on a new line/pipe per field needing validation?