Nicolas
Form validation with LiveView / Surface
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
Marked As Solved
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?
- Change
<Field name="email" class="form-control w-1/2">to<Field name={{ :email }} class="form-control w-1/2"> - Change
<Field name="password" class="form-control w-1/2">to<Field name={{ :password }} class="form-control w-1/2">
0
Last Post!
miguels
Popular in Questions
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
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
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










