Hi,
Looking at LiveView and Surface examples I created a first test Component to a Login form (code below).
It appears that this code fails with the following error:
** (exit) an exception was raised:
** (ArgumentError) argument error
:erlang.atom_to_binary("email", :utf8)
(phoenix_ecto 4.2.1) lib/phoenix_ecto/html.ex:111: Phoenix.HTML.FormData.Ecto.Changeset.input_value/4
(phoenix_html 2.14.3) lib/phoenix_html/form.ex:482: Phoenix.HTML.Form.input_value/2
(phoenix_html 2.14.3) lib/phoenix_html/form.ex:825: Phoenix.HTML.Form.generic_input/4
(surface 0.4.0) lib/surface/components/form/email_input.ex:31: anonymous fn/7 in Surface.Components.Form.EmailInput.render/1
(phoenix_live_view 0.15.5) lib/phoenix_live_view/diff.ex:356: Phoenix.LiveView.Diff.traverse/6
I’m not used with forms validation using changeset. Can someone help me with this one ?
defmodule ScubananaWeb.Components.LoginForm do
defmodule FormData do
use Ecto.Schema
import Ecto.Changeset
@primary_key false
embedded_schema do
field :email, :string
field :password, :string
end
def changeset(form_data, attrs \\ %{}) do
form_data
|> cast(attrs, [:email, :password])
|> validate_required([:email, :password])
|> validate_email()
end
defp validate_email(changeset) do
changeset
|> validate_format(:email, ~r/^[^\s]+@[^\s]+$/,
message: "must have the @ sign and no spaces"
)
|> validate_length(:email, max: 160)
end
def validate(params) do
%FormData{}
|> changeset(params)
|> Map.put(:action, :insert)
end
end
defmodule Component do
use Surface.LiveComponent
alias Surface.Components.Form
alias Surface.Components.Form.{Field, Label, EmailInput, PasswordInput}
data changeset, :changeset, default: FormData.changeset(%FormData{})
prop submit, :event
def render(assigns) do
IO.inspect(assigns.changeset)
~H"""
<Form for={{ @changeset }} change="validate" submit={{ @submit }}>
<Field name="email" class="form-control w-1/2">
<Label class="label"><span class="label-text">Email</span></Label>
<EmailInput class="input input-bordered flex-grow"/>
</Field>
<Field name="password" class="form-control w-1/2">
<Label class="label"><span class="label-text">Password</span></Label>
<PasswordInput class="input input-bordered flex-grow"/>
</Field>
<div class="card-actions w-1/2 mt-4">
<button type="submit" class="btn btn-primary flex-grow">Login</button>
</div>
</Form>
"""
end
def handle_event("validate", params, socket) do
{:noreply, assign(socket, changeset: FormData.validate(params))}
end
end
end