I have a LiveView with a simple_form.
If the user selects that he want a TV, then he can also define how many.
I have read the other posts regarding phx-change on input, but I could not find a solution that helped me.
I tried to add phx-change to the input, as you can see here.
The event is triggered, but the value specified in phx-value-company_id is not passed along with the params.
From the logs:
I want this part of the form to be dynamic. The user should be able to specify the number of parts he wants.
I want to pass the ID of the component and the value defined in the input field, as arguments with the update_qty event.
I can definitely use a hidden field.
If I can update the value of the hidden field when the value of the number input is changed, then perhaps that value can be passed along when I submit the form?
Do you have any suggestions on how I can design that hidden field to deliver the number value?
doesn’t seem right. The value of the numeric field should be included. I think the problem might be that you don’t have a name attribute set on your input. This is why _target is undefined and the value of the field is not being sent.
The phx-value-*’ attributes won’t be included with a change event. The change event only includes form data.
I can think of a couple of options. You could set the name attribute to the id of the component, such as name={"qty_" <> component.id}. Then you’ll get the id in the params when the input changes: "qty_someid" => 2
Another option is to move the phx-change attribute to the form element. This will trigger the change event when any input changes but the whole form (including hidden fields, with the component id) will be included in the params. You already have this setup as validate so you could just handle the quantity field there.