Nicolas

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

Showing Posts 1 to 3

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">
Nicolas

Nicolas OP

Hi,
Tanks for you help, using atoms fixes the issue.

I guess surface examples need to be fixed too. :wink:

miguels

miguels

Glad it worked :smiley:

We’ll fix the examples and docs asap!

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews