How to create dynamic cascade form in Symfony?
If I understand your problem correctly then you need to get child categories for given categories. You can pass it as a option to ProductType
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use AppBundle\Entity\ProductCategory;
use AppBundle\Repository\ProductCategoryRepository;
class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$category = $options['category'];
$builder->add('productCategory',EntityType::class,array(
'class' => 'AppBundle:ProductCategory',
'query_builder' => function(ProductCategoryRepository $repository) use ($category){
return $repository->getRootNodesQueryBuilder($category);
},
'label' => 'product category',
'choice_label' => 'name',
'choice_value' => 'name',
'multiple' => false,
'expanded' => false,
'required' => true
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Product::class,
'method' => 'POST',
'category' => null
));
}
}
In getRootNodesQueryBuilder() you can parse child categories based upon passed category. In controller (where you receive AJAX request), you can use it like
$form = $this->createForm(ProductType::class, $product, [
'category' => $request->get('category')
]);