PHP code example of imatic / form-bundle

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

    

imatic / form-bundle example snippets

 php


use Imatic\Bundle\FormBundle\Form\Type\AjaxEntityChoiceType;

class ExampleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('author', AjaxEntityChoiceType::class, [
            'class' => 'MyExampleBundle:User',
            'route' => 'app_example_example_autocomplete',
        ]);
    }
}
 php


namespace App\Form\Extension;

use Imatic\Bundle\FormBundle\Form\Extension\DatepickerExtension;
use Symfony\Component\OptionsResolver\OptionsResolver;

class DateTypeExtension extends DatepickerExtension
{
    public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);

        $resolver->setDefaults([
            'format' => 'dd.MM.yyyy',
            'date_format' => 'DD.MM.YYYY',
            'config_locale' => [
                'en' => [
                    'week' => ['dow' => 1],
                ],
            ],
        ]);
    }

    public static function getExtendedTypes(): iterable
    {
        return [DateType::class];
    }
}
 php


public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('collection', CollectionType::class, [
        'data_index' => $entity->getCollection()->count()
    ]);
}
 php


class ExampleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // example: setting template of child form (field)
        $builder->add('example', null, [
            // override form theme template
            'template' => 'MyBundle:Form:example_theme.html.twig',

            // pass extra variables to the theme templates when this field is rendered
            'template_parameters' => [
                'foo' => 'bar',
            ],
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        // example: setting template of the form type itself
        $resolver->setDefaults([
            'template' => 'MyBundle:Form:example_theme.html.twig',
            'template_parameters' => ['foo' => 'bar'],
        ]);
    }
}
 php


use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Imatic\Bundle\FormBundle\Form\DataTransformer\EmptyEntityToNullTransformer;

/**
 * Address type
 */
class AddressType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('street')
            ->add('number')
            ->add('city')
            ->add('postalCode', 'text')
        ;

        $builder->addModelTransformer(new EmptyEntityToNullTransformer(
            array_keys($builder->all())
        ));
    }

    // ...
}