PHP code example of a2lix / auto-form-bundle

1. Go to this page and download the library: Download a2lix/auto-form-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

a2lix / auto-form-bundle example snippets


// ...

class TaskController extends AbstractController
{
    public function new(Request $request): Response
    {
        $task = new Task(); // Any entity or DTO
        $form = $this
            ->createForm(AutoType::class, $task)
            ->add('save', SubmitType::class)
            ->handleRequest($request)
        ;

        // ...
    }
}

// ...

class TaskController extends AbstractController
{
    public function new(Request $request, FormFactoryInterface $formFactory): Response
    {
        $product = new Product(); // Any entity or DTO
        $form = $formFactory->createNamed('product', AutoType::class, $product, [
            // 1. Optional define which properties should be excluded from the form.
            // Use '*' for an "exclude-by-default" strategy.
            'children_excluded' => ['id', 'internalRef'],

            // 2. Optional define which properties should be rendered as embedded forms.
            // Use '*' to embed all relational properties.
            'children_embedded' => static fn (mixed $current) => [...$current, 'category', 'tags'],

            // 3. Optional customize, override, or add fields.
            'children' => [
                // Override an existing property with new options
                'description' => [
                    'child_type' => TextareaType::class, // Force a specific form type
                    'label' => 'Product Description', // Standard form options
                    'priority' => 10, 
                ],

                // Add a field that does not exist on the DTO/entity
                'terms_and_conditions' => [
                    'child_type' => CheckboxType::class,
                    'mapped' => false,
                    'priority' => -100,
                ],

                // Completely replace a field's builder with a callable
                'price' => function(FormBuilderInterface $builder, array $propAttributeOptions): FormBuilderInterface {
                    // The callable receives the main builder and any options from a potential attribute.
                    // It must return a new FormBuilderInterface instance.
                    return $builder->create('price', MoneyType::class, ['currency' => 'EUR']);
                },

                // Add a new field to the form
                'save' => [
                    'child_type' => SubmitType::class,
                ],
            ],

            // 4. Optional final modifications on the complete form builder.
            'builder' => function(FormBuilderInterface $builder, array $classProperties): void {
                // This callable runs after all children have been added.
                if (isset($classProperties['code'])) {
                    $builder->remove('code');
                }
            },
        ])->handleRequest($request);

        // ...
    }
}


use A2lix\AutoFormBundle\Form\Attribute\AutoTypeCustom;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

class Product
{
    #[AutoTypeCustom(excluded: true)]
    public private(set) int $id;

    public ?string $name = null;

    #[AutoTypeCustom(type: TextareaType::class, options: ['attr' => ['rows' => 5]])]
    public ?string $description = null;

    #[AutoTypeCustom(embedded: true)]
    public Category $category;
}

$form = $this->createForm(AutoType::class, $product, [
    'children_groups' => ['product:edit'],
]);

// ...
'children' => [
    'name' => [
        'child_groups' => ['product:edit', 'product:create'],
    ],
    'stock' => [
        'child_groups' => ['product:edit'],
    ],
],
// ...

use A2lix\AutoFormBundle\Form\Attribute\AutoTypeCustom;

class Product
{
    #[AutoTypeCustom(groups: ['product:edit', 'product:create'])]
    public ?string $name = null;

    #[AutoTypeCustom(groups: ['product:edit'])]
    public ?int $stock = null;
}

'children' => [
    '_' => function (FormBuilderInterface $builder): FormBuilderInterface {
        return $builder
            ->create('validity_range', FormType::class, ['inherit_data' => true])
                ->add('startsAt', DateType::class, [/* ... */])
                ->add('endsAt', DateType::class, [/* ... */]);
    },
]