Download the PHP package a2lix/auto-form-bundle without Composer
On this page you can find all versions of the php package a2lix/auto-form-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download a2lix/auto-form-bundle
More information about a2lix/auto-form-bundle
Files in a2lix/auto-form-bundle
Package auto-form-bundle
Short Description Automate form building
License MIT
Homepage https://github.com/a2lix/AutoFormBundle
Informations about the package auto-form-bundle
A2lix AutoForm Bundle
Stop writing boilerplate form code. This bundle provides a single, powerful AutoType form type that automatically generates a complete Symfony form from any PHP class.
[!NOTE] If you need to manage form translations, please see the A2lix TranslationFormBundle, which is designed to work with this bundle.
[!TIP] A complete demonstration is also available at a2lix/demo.
Installation
Use Composer to install the bundle:
Basic Usage
The simplest way to use AutoType is directly in your controller. It will generate a form based on the properties of the entity or DTO you pass it.
How It Works
AutoType reads the properties of the class you provide in the data_class option. For each property, it intelligently configures a corresponding form field. This gives you a solid foundation that you can then customize in two main ways:
- Form Options: Pass a configuration array directly when you create the form.
- PHP Attributes: Add
#[AutoTypeCustom]attributes directly to the properties of your entity or DTO.
Options passed directly to the form will always take precedence over attributes.
Customization via Form Options
This is the most flexible way to configure your form. Here is a comprehensive example:
Customization via #[AutoTypeCustom] Attribute
For a more declarative approach, you can place the configuration directly on the properties of your DTO or entity. This keeps the form configuration co-located with your data model.
Conditional Fields with Groups
You can conditionally include fields based on groups, similar to how Symfony's validation_groups work. This is useful for having different versions of a form (e.g., a "creation" version vs. an "edition" version).
To enable this, pass a children_groups option to your form. This option specifies which groups of fields should be included.
You can then assign fields to one or more groups using either form options or attributes.
Via Form Options
Use the child_groups option within the children configuration:
In this example, if children_groups is set to ['product:edit'], both name and stock will be included. If it's set to ['product:create'], only name will be included.
Via #[AutoTypeCustom] Attribute
Use the groups property on the attribute:
If no children_groups option is provided to the form, all fields are included by default, regardless of whether they have groups assigned.
Advanced Recipes
Creating a Compound Field with inherit_data
You can use a callable in the children option to create complex fields that map to the parent object, which is useful for things like date ranges.
Global Configuration
While not required, you can configure the bundle globally. For example, you can define a list of properties to always exclude.
Create a configuration file in config/packages/a2lix_auto_form.yaml:
License
This package is available under the MIT license.
All versions of auto-form-bundle with dependencies
symfony/config Version ^7.4|^8.0
symfony/dependency-injection Version ^7.4|^8.0
symfony/form Version ^7.4|^8.0
symfony/http-kernel Version ^7.4|^8.0
symfony/property-info Version ^7.4|^8.0
symfony/type-info Version ^7.4|^8.0
phpdocumentor/reflection-docblock Version ^5.6|^6.0