dev-rroslan
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
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
polypush135
First issue:
Your map has atoms for keys not string literals.
IE:
You sent
it wants
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 itheres you issue.
should look like
Your to_form function is taking a map that has an atom.
At the end of the day these are just warnings.
dev-rroslan
I am getting error:
** (KeyError) key :country_issued_id not found in: %{“country_issued_id” => “”}
polypush135
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_idfound 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.
Hermanverschooten
You are accessing
:country_issued_idby way of@form.params.country_issued_id, that hash doesn’t have that key. You don’t even need thatvalue-assignment, thefieldabove handles that already. Look at how the code inCoreComponents.inputextracts the value and assigns the key, so you don’t have to.