LiveView: submit form and update URL

Hi, what is the correct way to update URL params upon submitting a form, please?

I have a search input field with standard submit button, that has phx-submit binding. But I want pagination on the search results, too.

Generally one would probably go to /users/search-results and then have prev/next live_patch links there.

But what if I wanted to stay on the same page, and just update params like /users?search=john&page=1&per_page=10? Therefore the whole thing working more like a text filter?

Basically, how do I get the value from form textfield into URL, please?

Thank you

In handle_event call push_patch(socket, to: Routes.live_path(socket, Something, %{search: "John", page: 1, per_page: 10}). Then you do the validation and assigning of params in handle_params.

https://hexdocs.pm/phoenix_live_view/live-navigation.html

1 Like

so I use

  1. phx-submit binding to invoke handle_event and
  2. then from handle_event I call push_patch? and
  3. after push_patch the handle_params is actually called to complete this chain of calls and update socket?

Another post on this topic: LiveView push_patch append to params - #8 by josevalim

And yes to the above question.

1 Like

ok, thank you!