Best practices to send back the value of the form in LiveView

Hi, I wanted to create a search form in LiveView. To do that, I’m thinking of using form binding and phx_submit, as explained in guides. So far, it works, but the form value got submitted and gone.

I want to keep that value so the User knows what are they searching for. One way is to send back the form request through assign\3. But that means I have to use Ecto.Changeset instead of just ordinary map. Is this the best practice? Or can we do something more simple?

Changesets are cheap and as far as I know they are ordinary maps till evaluated for Repo action.

Best and most reliable way is changeset

If it is just a search term, You might not need a changeset, nor a form_for, just a search value.

I have one that looks like this.

    <form
      phx-submit="search">
      <div class="search-wrapper">
        <input
          type="search"
          name="search"
          value="<%= @search %>"
          placeholder="Search"
          autofocus
          autocomplete="off" />
        <button type="submit">
          Search
        </button>
      </div>
    </form>

You need to manage the value by yourself…

There is a search example in the free course on liveview from pragmatic studio, from @mikeclark and nicole.

Thank you. I use schemaless changesets for this and it works well.