PHP code example of kikwik / json-form-bundle

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

    

kikwik / json-form-bundle example snippets


// first model
namespace App\Model;

class Dimension
{
    private ?int $height = null;
    private ?int $width = null;
    
    // getter and setter...
}

// second model
namespace App\Model;

class TechData
{
    private ?string $someData = null;
    private ?string $otherData = null;
    
    // getter and setter...
}

// first model form
namespace App\Form\Model;

use App\Model\Dimension;

class DimensionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('height')
            ->add('width')
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class'=>Dimension::class
        ]);
    }
}

// second model form
namespace App\Form\Model;

use App\Model\TechData;

class TechDataType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('someData')
            ->add('otherData')
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class'=>TechData::class
        ]);
    }
}

// the entity
namespace App\Entity;

use App\Model\Dimension;

#[ORM\Entity(repositoryClass: ProductRepository::class)]
class Product
{    
    #[ORM\Column(type: Types::STRING)]
    private ?string $name = null;

    #[ORM\Column(type: 'json_document', nullable: true)]
    private ?Dimension $dimension = null;
    
    #[ORM\Column(type: 'json_document', nullable: true)]
    private array $techData = [];
    
    #[ORM\Column(type: Types::DATETIME_MUTABLE)]
    protected $updatedAt;
    
    // getter and setter...
}

// the entity form
namespace App\Form;

use Kikwik\JsonFormBundle\EventListener\JsonDocumentFormSubscriber;

class ProductFormType extends AbstractType
{
    public function __construct(private JsonDocumentFormSubscriber $jsonDocumentFormSubscriber) # autowire here
    {
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            ->add('dimension',DimensionType::class)
            ->addEventSubscriber($this->jsonDocumentFormSubscriber) # add as event subscriber
        ;
    }
     
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Product::class,
        ]);
    }
}

namespace App\Form;

use Kikwik\JsonFormBundle\EventListener\JsonDocumentFormSubscriber;

class ProductFormType extends AbstractType
{
    public function __construct(private JsonDocumentFormSubscriber $jsonDocumentFormSubscriber)
    {
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            ->add('techData',JsonDocumentCollectionType::class, [
                'model_labels' => [
                    'App\Model\Dimension' => 'Size of product',
                    'App\Model\TechData' => 'Technical data',
                ],
                'model_options' => [
                    'App\Model\Dimension' => ['unitOfMeasurement'=>'mm'],
                ]
            ])
            ->addEventSubscriber($this->jsonDocumentFormSubscriber)
        ;
    }
     
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Product::class,
        ]);
    }
}

namespace App\Form;

use Kikwik\JsonFormBundle\EventListener\JsonDocumentFormSubscriber;

class ProductFormType extends AbstractType
{
    public function __construct(private JsonDocumentFormSubscriber $jsonDocumentFormSubscriber)
    {
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            ->add('techData',JsonDocumentCollectionType::class, [
                'model_labels' => [
                    'App\Model\Dimension' => 'Size of product',
                    'App\Model\TechData' => 'Technical data',
                ],
                'model_options' => [
                    'App\Model\Dimension' => ['unitOfMeasurement'=>'mm'],
                ]
            ])
            ->addEventSubscriber($this->jsonDocumentFormSubscriber)
        ;
        
        $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'onPreSetData']);
    }
     
    public function onPreSetData(PreSetDataEvent $event)
    {
        /** @var Product $product */
        $product = $event->getData();
        $form = $event->getForm();

        if(!$product || $product->getId() == null)
        {
            $form->add('techData',JsonDocumentCollectionType::class, [
                'model_labels' => [
                    'App\Model\Dimension' => 'Size of product',
                    'App\Model\TechData' => 'Technical data',
                ],
                'model_options' => [
                    'App\Model\Dimension' => ['unitOfMeasurement'=>'mm'],
                ],
                'data_models' => [
                    'App\Model\Dimension',
                    'App\Model\TechData'
                ]
            ]);
        }
    } 
     
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Product::class,
        ]);
    }
}