Nicolas

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

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?

  1. Change <Field name="email" class="form-control w-1/2"> to <Field name={{ :email }} class="form-control w-1/2">
  2. Change <Field name="password" class="form-control w-1/2"> to <Field name={{ :password }} class="form-control w-1/2">

Last Post!

miguels

miguels

Glad it worked :smiley:

We’ll fix the examples and docs asap!

Where Next?

Popular in Questions Top

Emily
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
PeterCarter
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
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
alice
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
siddhant3030
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
romenigld
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 Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
vertexbuffer
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
joeerl
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
axelson
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...
239 49134 226
New
gausby
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...
1207 40082 209
New
senggen
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

We're in Beta

About us Mission Statement