I’ve got a custom button in a form (choosing an image from a library). I’m struggling to write an event handler for that button. I started with update_params/3 as this seems to indicate that it should be used for such a situation.
This can be useful for things like customized inputs or buttons, that have special handlers in your live view. For example, if you have an appointment that expresses a list of available times in the UI, but the action just takes a single
time
argument, you can make each available time a button, like so:
<.button phx-click="time-selected" phx-value-time="<%= time %>" />
That function works if the form has been interacted with in some way (e.g. change handler that has populated “params” on the form).
But if this button click is the first interaction, the form’s params have not been established yet, which causes issues when update_params
does validate etc which seems to expect a full list of params for the form. For example, if there is some other form field that is required (in my case: “Name”), after running update_params
that field will now be blank and will fail validation, and try to clear that field.
I’ve tried the only_touched option and some other functions, like using “validate” with partial params, but I can’t find a solution.
I suspect I’m misunderstanding how the form should be used. Any tips?