Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

_zac_'s avatar
Level 12

Dynamic components with form fields / wire:model

Is it possible?

I would like to display a group of components based on previous user selections, with the dynamic components having form fields accessible to Livewire.

It might be easier to show in code:

if(CONDITION) {
      $this->sets[] = 'COMPONENT';
}
...
@foreach($this->sets as $set)
      <x-dynamic-component :component="$set" />
@endforeach

Any form fields would be nested within an Livewire property array.

#[Validate('')]
public array $formgroup = [];
<input type="text" name="formgroup.field1" wire:model="formgroup.field1" />
<input type="text" name="formgroup.field2" wire:model="formgroup.field2" />
...

I had hoped this would then be accessible in the formgroup array after submission, but I cannot get data back from the form inputs.

I have had some success with:

<form wire:submit="submit(Object.fromEntries(new FormData($event.target)))">

suggested at: https://github.com/livewire/livewire/issues/313

But this does not seem to work nicely with all form field types and it would be nice to retain Livewire validation etc.

Any suggestions would be awesome.

0 likes
1 reply
LaryAI's avatar
Level 58

To achieve dynamic components with form fields that are accessible to Livewire, you can follow these steps:

  1. Dynamic Component Rendering: Use Livewire's ability to render components dynamically. You can use the @foreach directive to loop through your components and render them.

  2. Wire Model Binding: Ensure that your form fields are correctly bound to Livewire properties using wire:model.

  3. Form Submission: Use Livewire's form submission capabilities to handle the form data.

Here's a step-by-step solution:

Livewire Component

  1. Define the Livewire Component: Create a Livewire component with a property to hold your dynamic components and form data.
  1. Blade View: Create a Blade view for your Livewire component.
<form wire:submit.prevent="submit">
    @foreach($sets as $set)
        <x-dynamic-component :component="$set" :formgroup="$formgroup" />
    @endforeach

    <button type="submit">Submit</button>
</form>

Dynamic Component

  1. Create a Dynamic Component: Define a Blade component that will be used dynamically.
<!-- resources/views/components/dynamic-component.blade.php -->
<div>
    <input type="text" wire:model="formgroup.field1" placeholder="Field 1">
    <input type="text" wire:model="formgroup.field2" placeholder="Field 2">
</div>

Explanation

  • Dynamic Components: The @foreach loop iterates over the $sets array, rendering each component dynamically.
  • Wire Model: The wire:model directive binds the input fields to the formgroup array in the Livewire component.
  • Form Submission: The wire:submit.prevent="submit" directive prevents the default form submission and calls the submit method in the Livewire component.

Validation

  • Validation: The submit method includes validation rules for the form fields. Adjust these rules based on your requirements.

This setup allows you to dynamically render components with form fields that are bound to Livewire properties, enabling you to handle form submissions and validations effectively.

Please or to participate in this conversation.