Customizing Forms
Last updated
Was this helpful?
Last updated
Was this helpful?
Forms in Sylius are flexible and extendable, allowing you to modify them to better fit your business needs.
You might need to customize a form in Sylius to: β Add new fields β e.g., a secondary phone number for customers. β Modify existing fields β change labels, make them required, or adjust their CSS classes. β Remove fields β get rid of unnecessary form fields.
Letβs say you want to customize the Customer Profile Form by:
Adding a secondaryPhoneNumber
field.
Removing the gender
field.
Changing the label for lastName
from sylius.form.customer.last_name
to app.form.customer.surname
.
Ensure Your Model Supports New Fields
Before adding a new field, make sure it exists in the model. For example, secondaryPhoneNumber
must be added to the Customer entity and properly mapped in your database. To learn how to do that, check .
Create a Form Extension
To find the base class of the form you want to extend, run:
This will return:
Remember: Using the form type that has been returned from the service with sylius.
prefix will apply the extension to both forms!
We need to extend Sylius\Bundle\ShopBundle\Form\Type\CustomerProfileType
.
Create a new form extension class:
If you add a new label, remember to define it in your translation files (translations/messages.en.yaml
).
Register the Form Extension
If autoconfiguration is disabled, register the extension in config/services.yaml
:
If autoconfiguration is enabled, no manual registration is needed.
You can check if the extension is correctly registered by running:
Make sure your form templates reflect the new changes:
Render the new fields you added.
Remove the old fields you removed.
Now you need to find the correct hook that is associated with the form you want to customize.
To do this, go to the page containing the form and use your browser's developer tools to inspect it. Look for the comments surrounding the form.
< β BEGIN HOOK | name: "sylius_shop.account.profile_update.update.content.main.form" β >
It means the hook we want to customize is:
Create an appropriate twig for the secondaryPhoneNumber
field:
Update the form hook with your new field template:
To remove the old field from the template, just find and disable the hook responsible for it:
Then your final result should look like:
Some forms in Sylius are already extended in the core system. Example:
ProductVariantType
is extended by ProductVariantTypeExtension
in Sylius/Bundle/CoreBundle/Form/Extension/
.
If you want to add another extension to an already extended form, define the priority:
Extensions with higher priority values run first.
Some form fields in Sylius are added dynamically using event listeners.
For example, the ProductVariantTypeExtension in CoreBundle
adds channelPricings
dynamically:
To modify or remove dynamically added fields, you need to listen for the same event and adjust the form accordingly:
β You can apply all these form customizations directly in your application or as part of a Sylius plugin. β If you're customizing forms frequently, using extensions is recommended to avoid overriding entire forms.
With this guide, you should have a solid understanding of how to customize forms in Sylius to match your projectβs needs! π
Find out more about the amazing features of Twig Hooks