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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
Blokh
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
roeland
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
subsaharancoder
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
mohsen
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
jaybe78
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

Other Trending Topics Top

JesseHerrick
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews