How to dynamically build a form based on a TreeBuilder object?

I want to define a configuration scheme for my class and override them using administration options. To do this, I need a form to capture data from the administrator.

In Symfony Configuration Component the TreeBuilder class TreeBuilder responsible for defining the configuration scheme. and, as you know, Form Component has a tree similar to a structure similar to TreeBuilder .

How to dynamically make a Form object based on an instance of TreeBuilder ?

+8
php forms symfony configuration
source share
1 answer

Your woodworker or part of it should be iterable. Therefore, allowing it to be presented as rigorous as possible, you can use it to easily match the configuration with the developer. The easiest way would be to use the yml format:

 form: name: 'exampleForm' path: 'target_path' fields: fieldName: type: 'TextType' attr: # some additional options otherFieldName: type: 'TextType' attr: # some additional options 

See the configuration component processing section for more information: http://symfony.com/doc/current/components/config/definition.html#processing-configuration-values

The processed configuration can then be processed using the factory form and will probably look like this:

 $config = $configuration->processConfiguration($config, FormType::class, null, $config['path']); $formBuilder = $container->get('form.factory')->createNamedBuilder($config['name'); foreach ($config['fields'] as $field) { $formBuilder->add($fieldName, $field['type'], $field['attr']); } $form = $formBuilder->createForm(); 
+1
source share

All Articles