Nicolas
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
I’m using an Umbrella project for a Phoenix application, and I want to have one Ecto Repo and one PostgreSQL database shared by all apps....
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
miguels
Hi @Nicolas!
When using changesets with your form you need to pass atoms as your field names.
Could you try following and let me know if that fixes your issue?
<Field name="email" class="form-control w-1/2">to<Field name={{ :email }} class="form-control w-1/2"><Field name="password" class="form-control w-1/2">to<Field name={{ :password }} class="form-control w-1/2">Nicolas
Hi,
Tanks for you help, using atoms fixes the issue.
I guess surface examples need to be fixed too.
miguels
Glad it worked
We’ll fix the examples and docs asap!