Skip to content

LiveListener event is also triggering component form submit #3287

@FluffyDiscord

Description

@FluffyDiscord

I have live component with form

<?php

#[AsLiveComponent]
class AddVoucherForm extends AbstractController
{
    use DefaultActionTrait;
    use ComponentWithFormTrait;
    use ComponentToolsTrait;

    public bool $discountApplied = false;

    protected function instantiateForm(): FormInterface
    {
        return $this->createForm(AddVoucherType::class);
    }

    public function mount(): void
    {
        $this->loadDiscountState();
    }

    #[LiveListener(GlobalTwigEvent::ORDER_CHANGED->value)]
    public function onOrderChanged(): void
    {
        $this->loadDiscountState();
    }

    #[LiveAction]
    public function remove(): void
    {
        // do stuff

        $this->emit(GlobalTwigEvent::ORDER_CHANGED->value);
    }

    #[LiveAction]
    public function save(): void
    {
        $this->submitForm();

        // do stuff

        $this->emit(GlobalTwigEvent::ORDER_CHANGED->value);

        $this->resetForm();
    }

    private function loadDiscountState(): void
    {
        $order = $this->currentOrderProvider->provide();
        $this->discountApplied = !$order->getPromotions()->isEmpty();
    }
}

Now when I trigger the remove action by clicking my button in this component

<div class="space-y-4" {{ attributes }}>
    {% if discountApplied %}
        <button 
                type="button"
                data-action="live#action"
                data-live-action-param="remove"
        >Change coupon</button>
    {% else %}
        {{ form_start(form, {
            'attr': {
                'data-action': 'live#action:prevent',
                'data-live-action-param': 'save',
            },
        }) }}
            my form stuff
            {{ form_rest(form) }}
        {{ form_end(form) }}
    {% endif %}
</div>

I can see that it correctly triggers the GlobalTwigEvent::ORDER_CHANGED event, sending it as a POST also to this component (that's fine, it has a listener for it after all), but instead of only running the listener logic, it also handles the form for some reason, getting back status code 422 because of course it's invalid, no form/fields have been submitted. This happens only when the form has been sent at least once.

Image

I would expect it to just run the listener itself, no form submission should take place

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions