Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I seem to pass a variable from a controller to a form in symfony 4?

Tags:

symfony

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'

like image 702
Pierre Avatar asked Nov 06 '22 15:11

Pierre


1 Answers

So I eventually got it straightened up. Two things :

  • I forgot to mention was that this form type was an embedded child of a parent form : so I had to set the variable as default options in the same way, but upstream, then pass it as one custom index of the parameters array entry_options of CollectionType.
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']);

    }
}

  • Once the variable was hydrating the child form, the repository method called from the query_builder parameter would make the form throw a format error (the 'array given' sorta thing) : the reason was, since this was an existing method in the repository, called by other controllers, it contained ->getQuery() and ->getResult() querybuilder sub-methods, where breaking the form.
    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

like image 198
Pierre Avatar answered Nov 15 '22 10:11

Pierre