Skip to content

[LiveComponent] Form input in focus does not reset after submit by hitting enter #2438

Open
@michilehr

Description

@michilehr

Given I have a form with one field which should reset after submit, the field is not updated (emptied in my case) after hitting the enter button. By clicking the submit button, it works.

Any chance to solve this without writing JavaScript?

Thanks for any help!

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

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

    protected function instantiateForm(): FormInterface
    {
        // we can extend AbstractController to get the normal shortcuts
        return $this->createForm(DeviceBarcodeType::class, ['barcode' => '']);
    }

    private function getDataModelValue(): ?string
    {
        return 'norender|*';
    }
}
<div>
    {{ form_start(form, {
        attr: {
            'data-action': 'live#action:prevent',
            'data-live-action-param': 'submit',
        }
    }) }}
    {{ form_row(form.barcode) }}
    <button
            type="submit"
    >Submit
    </button>
    {{ form_end(form) }}
</div>

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