PHP code example of wemxo / dynamic-form-bundle

1. Go to this page and download the library: Download wemxo/dynamic-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/ */

    

wemxo / dynamic-form-bundle example snippets




return [
    /* ... */
    Wemxo\DynamicFormBundle\DynamicFormBundle::class => ['all' => true],
    /* ... */
];




return [
    'france' => [
        'form' => [
            'subscribers' => NULL,
            'fields' => [
                'address' => [
                    'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType',
                    'options' => [
                        'label' => 'Address',
                        '    'options' => [
                                'message' => 'You must enter your address',
                            ],
                        ],
                    ],
                ],
                'addressComplement' => [
                    'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType',
                    'options' => [
                        'label' => 'Address complement',
                        'nts\\NotBlank',
                            'options' => [
                                'message' => 'You must enter your postal code',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
    'italy' => [
        'form' => [
            'subscribers' => NULL,
            'fields' => [
                'address' => [
                    'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType',
                    'options' => [
                        'label' => 'Address',
                        '    'options' => [
                                'message' => 'You must enter your address',
                            ],
                        ],
                    ],
                ],
                'addressComplement' => [
                    'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType',
                    'options' => [
                        'label' => 'Address complement',
                        'nts\\NotBlank',
                            'options' => [
                                'message' => 'You must enter your postal code',
                            ],
                        ],
                    ],
                ],
                'department' => [
                    'type' => 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType',
                    'options' => [
                        'label' => 'Department',
                        '        'constraints' => [
                        0 => [
                            'class' => 'Symfony\\Component\\Validator\\Constraints\\NotBlank',
                            'options' => [
                                'message' => 'You must enter your city',
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];



declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Wemxo\DynamicFormBundle\Form\DynamicType;
use Wemxo\DynamicFormBundle\Utils\Helper\DynamicFormHelper;

class AppController extends AbstractController
{
    #[Route(name: 'app_home')]
    public function home(FormFactoryInterface $formFactory): Response
    {
        $user = $this->getUser();
        $countryLabel = $user->getCountry()->getLabel();
        $form = $formFactory->create(DynamicType::class, null, [
            'dynamic_key' => DynamicFormHelper::configKey('address', $countryLabel),
        ]);

        return $this->render('app/home.html.twig', [
            'form' => $form->createView()
        ]);
    }
}



declare(strict_types=1);

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Wemxo\DynamicFormBundle\Form\DynamicType;
use Wemxo\DynamicFormBundle\Utils\Helper\DynamicFormHelper;

class AppController extends AbstractController
{
    #[Route(name: 'app_home')]
    public function home(FormFactoryInterface $formFactory): Response
    {
        $user = $this->getUser();
        $countryLabel = $user->getCountry()->getLabel();
        $form = $formFactory->createNamed('my_custom_prefix', DynamicType::class, null, [
            'dynamic_key' => DynamicFormHelper::configKey('address', $countryLabel),
        ]);

        return $this->render('app/home.html.twig', [
            'form' => $form->createView()
        ]);
    }
}



declare(strict_types=1);

namespace App\Form;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Wemxo\DynamicFormBundle\EventSubscriber\DynamicFormEventSubscriber;

class AddressFormEventSubscriber implements DynamicFormEventSubscriber
{

    public function getName(): string
    {
        return 'address_event_subscriber';
    }

    public static function getSubscribedEvents(): array
    {
        return [
            FormEvents::PRE_SET_DATA => 'onPreSetData',
        ];
    }
    
    public function onPreSetData(FormEvent $event): void
    {
        // do something.
    }
}