No case clause matching: {1234567890, :graphemes}

I am struggling with the error message no case clause matching: {1234567890, :graphemes}. The following are excepts from my code:

defmodule Agency.Client do
  use Ecto.Schema
  import Ecto.Changeset

  schema "clients" do
    field(:name, :string)
    field(:address, :string)
    field(:contact, :string)
    field(:tel_number, :integer)
    timestamps()
  end

  def changeset(client, params \\ %{}) do
    client
    |> cast(params, [:name, :address, :contact, :tel_number])
    |> validate_required([:name, :address, :contact, :tel_number])
    |> validate_length(:name, min: 3)
    |> validate_length(:address, min: 5)
    |> validate_length(:contact, min: 3)
    |> validate_length(:tel_number, min: 8)
    |> validate_change(:name, &validate/2)
  end
end
defmodule Agency do
  alias Agency.{Client, Role}

  @repo Agency.Repo

  def insert_client(attrs) do
    %Client{}
    |> Client.changeset(attrs)
    |> @repo.insert()
  end
end
defmodule AgencyWeb.ClientController do
  use AgencyWeb, :controller

  def index(conn, _params) do
    clients = Agency.list_clients()
    render(conn, "index.html", clients: clients)
  end

  def new(conn, _params) do
    client = Agency.new_client()
    render(conn, "new.html", client: client)
  end

  def create(conn, %{"client" => client_params}) do
    {:ok, _client} = Agency.insert_client(client_params)
    redirect(conn, to: Routes.client_path(conn, :index))
  end
end

new.html.eex

<h1>New Client</h1>

<%= form_for @client, Routes.client_path(@conn, :create), fn f -> %>
  <%= label f, :name %>
  <%= text_input f, :name %>

  <%= label f, :address %>
  <%= text_input f, :address %>

  <%= label f, :contact %>
  <%= text_input f, :contact %>

  <%= label f, :tel_number, "Tel Number" %>
  <%= telephone_input f, :tel_number %>

  <div>
    <%= submit "Submit" %>
  </div>

<% end %>
Params

client

    %{"address" => "fsdfsdfs", "contact" => "sdfsdfsd", "name" => "sdfsfdsf", "tel_number" => "0123456789"}

I think its got something to do with converting string to integer but isn’t Ecto.Changeset.cast supposed to do that?

Please show the full stack trace.

1 Like

Eh. I am pretty sure this is a typo, it should be :string. Also, integer type does not have min: 8 guard, hence the error.

2 Likes

Thanks @mudasobwa for your assistance. Indeed after I changed :integer to :string, rolled back and recreated to table, it’s working now.

A related question, why does integer not have min:? I if I need to validate that an integer field has a certain number of digits in what are my options? Do I have to create a custom validation for that or there is one available?

:min in what sense?

Have you considered using :less_than or :greater_than?

https://hexdocs.pm/ecto/Ecto.Changeset.html#validate_number/3

You know what now that I’m trying to find a concrete example in responds to your question, I’m struggling to come up with one. I was going to give a date example but you’ve got :datetime. So I’ll just run away with my tail between my legs and forget I asked. :smile:

https://hexdocs.pm/ecto/Ecto.Changeset.html#validations/1

Thanks @mudasobwa.