#Message input field not being cleared on save - phoenix and liveview

9 messages · Page 1 of 1 (latest)

slender niche
#

I am on the latest version of liveview and phoenix (updated myself), I'm using the core_components that get's generated when you use phoenix generators.

My input field is not getting reset to Your message... on save.

On save I set the @new_message to to_form(create_new_message(user.user_id)) in the save event_handler. Any advice on how I can debug this is appreciated

my render function

 <%= if assigns[:current_user] do %>
        <.simple_form
          for={@new_message}
          class="footer-newsletter-form relative"
          phx-submit="save"
          phx-change="validate"
        >
          <.input
            type="text"
            field={@new_message[:content]}
            placeholder="Your message..."
            class="text-lgblock w-[400px] text-[14px] h-[50px] pl-[25px]  py-[17px] rounded-md border-none bg-[#1f2935] placeholder:text-[14px] focus:ring-[none] focus:!border-none "
          />
          <:actions>
            <button class=" absolute w-[63px] text-[28px] text-[#1f2935] flex items-center justify-center p-2.5 rounded-md border-[none] right-0 top-0 bg-[#45f882] hover:bg-[#ffbe18] ">
              <i class="flaticon-paper-plane"></i>
            </button>
          </:actions>
        </.simple_form>
...

In the event handler (last thing that happens)

{:noreply,
       socket
       |> assign(:new_message, to_form(create_new_message(user.user_id)))
vocal canopy
#

that's in the phx-submit event handler? what does create_new_message do and return?

#

can you show more of the code?

#

if you want to reset the form to empty, then you need to set @new_message back to an empty form, something like to_form(empty_message_changeset)

slender niche
#

@vocal canopy Yes.

create_new_message(user.user_id)) creates a changeset:

  defp create_new_message(user_id) do
    %ChatMessage{
      content: "",
      id: UUID.uuid4(),
      user_id: user_id
    }
    |> ChatMessage.new_changeset()
  end
slender niche
#

I think I can clear it with javascript via a hook, I'm out of ideas 😄

slender niche
#

So, I updated Phoenix. Maybe that's why it stopped working 🧐

#

I solved it

#

I added id: as an argument to the to_form function on save