Reputation: 67
I have in my ListProductsController variable $parentId. I want to get $parentId value and to use it in my SearchProductType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price',EntityType::class,[
'class'=>Product::class,
'choice_label'=>'price',
'choice_value'=>'price',
'placeholder'=>'Default',
'query_builder' => function (EntityRepository $er){
return $er->createQueryBuilder('product')
->innerJoin('product.category','c')
->addSelect('c')
->innerJoin('product.manorwomen','m')
->addSelect('m')
->where('c.parent_id=1')
},
'expanded'=>false,
'multiple'=>false
])
->add('submit',SubmitType::class)
;
}
c.parent_id must be equal to $parentId from controller
->where('c.parent_id=$parentId')
How to do that?
Upvotes: 0
Views: 1933
Reputation: 29912
Pass it as a required (mandatory) option to your SearchProductType
form
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired([
'parentId',
]);
}
Then pass it when creating the form in ListProductsController
$form = $this->createForm(SearchProductType::class, $objName,
['parentId' => $parentId], //or whatever the variable is called
);
Finally use it
public function buildForm(FormBuilderInterface $builder, array $options)
{
$parentId = $options['parentId'];
$builder->add('price',EntityType::class,[
[...]
'query_builder' => function (EntityRepository $er) use ($parentId) {
[...]
->where('c.parent_id=' . $parentId)
},
]);
}
Upvotes: 8
Reputation: 3483
HI can you try like this
controller
$data = array("parentId" => $parentId));
$form = $this->createForm(new ExampleFormType($data), $objName);
form
class ExampleFormType extends AbstractType {
public $data = array();
public function __construct($data) {
$this->data = $data; // Now you can use this value while creating a form field for giving any validation.
}
}
you can access parentId from $data array
Upvotes: 0