Generate token using simple form

I am given warning here:
warning: a map with atom keys was given to a form. Maps are always considered parameters and therefore must have string keys, got: %{country_issued_id: “”}

and another warning:
warning: variable “country_issued_id” is unused (if the variable is not meant to be used, prefix it with an underscore)

Below is my code, hope someone will help me:
defmodule UndiWeb.GenerateLive do
use UndiWeb, :live_view

alias Undi.Tokens.Token

alias Undi.Tokens

@impl true
def render(assigns) do
~H"“”
<.header>
Generate Token for login to survey
<:subtitle>From ID we will obtain your age and gender</:subtitle>
</.header>

<div>
  <.simple_form for={@form} id="generate_link_form" phx-change="validate" phx-submit="save_token">
    <.input
      field={@form[:country_issued_id]}
      value={@form.params.country_issued_id}
      type="text"
      label="MyKad"
    />

    <:actions>
      <.button phx-disable-with="Generating...">Generate Token</.button>
    </:actions>
  </.simple_form>
</div>
"""

end

@impl true
def mount(_params, _session, socket) do
{:ok,
assign(socket,
form: to_form(%{country_issued_id: “”})
)}
end

@impl true
def handle_event(“save_token”, %{“country_issued_id” => country_issued_id} = p, socket) do
case Tokens.create_token(p) do
{:ok, _token} →
{
:noreply,
socket
|> put_flash(:info, “Record created successfully”)
}

  {:error, %Ecto.Changeset{} = changeset} ->
    {:noreply, assign_form(socket, changeset)}
end

end

@impl true
def handle_event(“validate”, _, socket) do
{:noreply, socket}
end

defp assign_form(socket, %Ecto.Changeset{} = changeset) do
assign(socket, :form, to_form(changeset))
end
end

First issue:

Your map has atoms for keys not string literals.
IE:
You sent

%{country_issued_id: “”}

it wants

%{"country_issued_id" => “”}

Because they are parameters are possibly coming in from outside and someone could possible run out of atoms causing their app to crash. Or put another way there would be no way to limit the number of atoms the system could possibly have to make if anyone can just submit the form and pass in a new key in the form of an atom. Thus why they warning you to use strings.

Also try to look up how to use the ` char in these posts to format your code sections. It will help others read it better.

The next warning is self explanatory. You are not using the thing thus either annotate it with a prefix of _or remote it

heres you issue.

{:ok,
assign(socket,
form: to_form(%{country_issued_id: “”})
)}

should look like

{:ok,
assign(socket,
form: to_form(%{"country_issued_id" => “”})
)}

Your to_form function is taking a map that has an atom.

At the end of the day these are just warnings.

I am getting error:
** (KeyError) key :country_issued_id not found in: %{“country_issued_id” => “”}

Do you know why its saying that?

its self explaining.
I don’t even have the full context and I can see from what you said that there is no atom key of :country_issued_id found inside of this map %{“country_issued_id” => “”}

So somewhere in your code is assuming this map will have a atom key of :country_issued_id thus you should fix that since its now a string.

Also maybe go read this https://arunramgt.medium.com/elixir-basics-of-atoms-9ff6c48463d6

I personally think its very important to have a fundamental understanding of atoms and strings. This is really at the core of your issue.

1 Like

You are accessing :country_issued_id by way of @form.params.country_issued_id, that hash doesn’t have that key. You don’t even need that value-assignment, the field above handles that already. Look at how the code in CoreComponents.input extracts the value and assigns the key, so you don’t have to.