How to add a live form for a custom model?

This guide shows how to integrate Symfony UX Live Components into a Sylius form for a custom model (resource). We’ll build a Supplier resource that:

  • Manages a collection of Zones via a Live Collection component

  • Offers an autocomplete for selecting a Channel via a Live Autocomplete component

Prerequisites

For the purpose of this guide, we'll be using the previously created Supplier resource in the examples.

Also, make sure you have an understanding of Symfony UX Live Components, which you can read about here:

Build your Live Components

Live form

Once you create the Supplier resource, it comes with a fully functional form type. However, it lacks features like dynamic validation. To add these, the default form maintained by the resource bundle needs to be transformed into a dynamic one.

Constraints

Make sure the Supplier resource includes constraints that can be validated:

Register live component

First, we need to register the component. It’s important to set the correct tag. As we are dealing with the resource in the admin context (a Supplier resource managed by an admin user), the component must be registered to reflect this context accordingly.

The service has been registered, however, it's not yet used anywhere.

The next step is to override the generic hookables that render the component with the live version we created:

We need to provide:

From now on, when you focus on a required field, dynamic validation will be triggered once the field loses focus, without the need to explicitly submit the form:

Live collection

Expanding the Supplier Entity

First, let’s expand the Supplier entity by adding a new field called zones. The easiest way to do this is by using the MakerBundle to create a many-to-many relation with the App\Entity\Addressing\Zone entity through the wizard. Don’t forget to create and execute the migration afterwards.

Supplier FormType

Since we want to add a LiveCollectionType to our entity, we can no longer rely on the default form type from the resource bundle. Let's to create a new one:

The Supplier configuration needs to be updated accordingly:

The component also needs to be updated to use the new SupplierType form type:

Organization of form fields

When you want to render all defined form fields as they are, no special adjustments are needed—the internal code handles everything. However, if you’d like to change the order or exclude certain fields from rendering, you can follow Symfony’s documentation. For example, you can adjust the order in the form type configuration or override the form rendering template using Twig hooks to manually render selected fields with {{ form_row(...) }}, and so on.

Live autocomplete

Expanding the Supplier Entity again

Let’s expand the Supplier entity by adding another field called channels with a many-to-many relation with the App\Entity\Channel\Channel entity through the Symfony Maker wizard and handle the migration afterwards.

Channel Autocomplete Type

Take a look at the configured route: sylius_admin_entity_autocomplete. It must be set appropriately to reflect usage within the admin context.

Supplier FormType

Include channels field in the form type:

Ready to go. No further changes needed.

Last updated

Was this helpful?