# Generate token using simple form

**URL:** <https://forum.elixirforum.com/t/generate-token-using-simple-form/60995>\
**Category:** Questions\
**Tags:** phoenix, liveview\
**Created:** [January 15, 2024, 2:52pm UTC](https://forum.elixirforum.com/t/generate-token-using-simple-form/60995 "2024-01-15T14:52:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev-rroslan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dev-rroslan/32/24785_2.png) [@dev-rroslan](https://forum.elixirforum.com/u/dev-rroslan)\
**Post date:** [January 15, 2024, 2:52pm UTC](https://forum.elixirforum.com/t/generate-token-using-simple-form/60995/1 "2024-01-15T14:52:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![polypush135](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@polypush135](https://forum.elixirforum.com/u/polypush135)\
**Post date:** [January 15, 2024, 3:01pm UTC](https://forum.elixirforum.com/t/generate-token-using-simple-form/60995/2 "2024-01-15T15:01:31Z")

</div>

> [@dev-rroslan](#):
>
> 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: “”}

First issue:

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

```elixir
%{country_issued_id: “”}

```

it wants

```elixir
%{"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.

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

```

should look like

```elixir
{: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.

---

<div class="post-metadata">

**Author:** ![dev-rroslan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/dev-rroslan/32/24785_2.png) [@dev-rroslan](https://forum.elixirforum.com/u/dev-rroslan)\
**Post date:** [January 15, 2024, 3:28pm UTC](https://forum.elixirforum.com/t/generate-token-using-simple-form/60995/3 "2024-01-15T15:28:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![polypush135](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@polypush135](https://forum.elixirforum.com/u/polypush135)\
**Post date:** [January 15, 2024, 3:29pm UTC](https://forum.elixirforum.com/t/generate-token-using-simple-form/60995/4 "2024-01-15T15:29:21Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![Hermanverschooten](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hermanverschooten/32/6447_2.png) [@Hermanverschooten](https://forum.elixirforum.com/u/Hermanverschooten)\
**Post date:** [January 16, 2024, 4:34pm UTC](https://forum.elixirforum.com/t/generate-token-using-simple-form/60995/5 "2024-01-16T16:34:41Z")

</div>

> [@dev-rroslan](#):
>
> ```elixir
> <.input
> field={@form[:country_issued_id]}
> value={@form.params.country_issued_id}
> type="text"
> label="MyKad"
> />
> 
> ```

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.
