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 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,
]);
}
}