enkr1

enkr1

The fields of the inner form inputs_for got reset when modifying other fields

I have a map field which stores my mobile_json and I used inputs_for to display the embedded_schema’s fields which looks like:

<%= inputs_for f, :m_json, fn mobile_json -> %>
<div class="mobile-number-input-container">
  <div>
    <%= select mobile_json, :mcc, @country_dialing_codes %>
    <%= error_tag mobile_json, :mcc %>
  </div>
  <div>
    <%= number_input mobile_json, :mobile_nb, placeholder: "e.g. 90123456" %>
    <%= error_tag mobile_json, :mobile_nb %>
  </div>
</div>
<% end %>

This is what the form looks like:

Step 1: Fill in the one of the mobile_json fields

Step 2: Modify any other fields (e.g. Country)

As you can see the Mobile Number field reset back to empty. When you modify any other fields, the data in the inputs_for will be reset.

Really struggling to find solutions online, I really need help.

Thank you so so much in advance.

Marked As Solved

al2o3cr

al2o3cr

There’s something important missing from the call to new_user_changeset: the second parameter!

Note that LiveView will not overwrite the element you just edited, so it looks like only the last value you sent sticks around.

Last Post!

enkr1

enkr1

Thank you so much for guiding me in the right direction!

my solution (just for anyone who hits it in the future):


defp assign_changeset(socket, :edit), do: # ...
defp assign_changeset(socket, _), do: # ...
defp assign_changeset(socket, :validate, attrs),
  do:
    socket
    |> assign(
      :changeset,
      Accounts.update_user_changeset(socket.assigns.user, attrs)
      |> Map.put(:action, :validate)
    )

# My validate of my form handle_event
@impl true
def handle_event("validate", %{"user" => user_params} = data, socket) do
  {:noreply, socket |> assign_changeset(:validate, user_params)}
end

Just like what @al2o3cr said, my mistake was that I did not pass my changed attribute into the changeset, that was why it was always reset back to what it was.

Where Next?

Popular in Questions Top

New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement