So I am trying to pass a variable to a form from my controller in a Symfony 4.3 project... I have tried to set the options resolver, pass arrays... Im still getting no luck and the 'option "0" does not exist....'
define the variable in configureOptions as setDefaults and setRequired pass an array () type from the controller in the createForm(ThingType::class, $thing, array ('my_option' => $myOption)
from the controller :
$form = $this->createForm(PieceEditType::class, $piece, ['pieceId'=> $piece->getId()]);
from the form side :
* @param FormBuilderInterface $builder
* @param array $options
*
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$pieceId = $options['pieceId'];
$builder
->add('full', FileType::class, [
'data_class' => null
])
->add('ordering', EntityType::class, [
'class' =>Image::class,
'choice_label' => 'ordering',
'query_builder' => function (ImageRepository $er){
return $er->findByPieceAndReturnOrdered($pieceId);
}
])
;
}
/**
* @param OptionsResolver $resolver
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Image::class,
'pieceId' => null,
));
$resolver->setRequired([
'pieceId',
]);
}
So just trying to pass a pieceId to the form for its own querybuilder so it populates the select with just what I need it to populate it with.
instead, error 'The option "0" (or "pieceId" ) does not exist. Defined options are etc etc'
So I eventually got it straightened up. Two things :
class PieceEditType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$pieceId = $options['pieceId'];
$builder
->add('images', CollectionType::class, [
'entry_type'=> ImagesEditType::class,
'allow_add' =>true,
'allow_delete' => true,
'by_reference' =>false,
'entry_options' => ['pieceId' => $pieceId,'label'=>false],
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Piece::class,
'pieceId'=> null,
]);
}
From there, the variable was passed to the child form without a change in the code (the error was just upstream).
Child form :
public function buildForm(FormBuilderInterface $builder, array $options)
{
$test = $options['pieceId'];
$builder
->add('ordering', EntityType::class, [
'class' =>Image::class,
'choice_label' => 'ordering',
'query_builder' => function (ImageRepository $er) use ($test){
return $er->findByPieceAndReturnOrdered($test);
}
])
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
['data_class' => Image::class,
'pieceId' => null,
]
); $definedOptions = $resolver->getDefinedOptions();
dump($definedOptions);
$resolver->setDefined('pieceId');
$resolver->setRequired(['pieceId']);
}
}
public function findByPieceAndReturnOrdered($pieceId)
{
return $this->createQueryBuilder('i')
->andWhere('i.Piece = :val')
->setParameter('val', $pieceId)
->orderBy('i.ordering', 'ASC')
->getQuery()
->getResult()
;
}
So I ended up writing a dedicated query function, right in the formtype, like a very lazy person, which allowed my form to finally be rendered.
'query_builder' => function (ImageRepository $er) use ($test){
return $er->createQueryBuilder('u')
->andWhere('u.Piece = :val')
->setParameter('val', $test)
->orderBy('u.ordering', 'ASC')
;
The getQuery() and getResult() just needed to be removed. Child form likes his queries with no training wheels.
Thanks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With