1

I would like to know how to pass my entity correctly and another variable in my createForm function ?

Let me explain, I would like to do this:

$repo = $em->getRepository('ProjectBundle:Organisation\Organisation');
            $list = $repo->children($this->getUser()->getOrganisation());
            $form = $this->createForm('ProjectBundle\Form\Robot\RobotType', array($entity,$list));

I need to pass to my FormType another variable, so I use a array directly.

My FormType:

<?php

namespace ProjectBundle\Form\Robot;

use ProjectBundle\Entity\Robot\Robot;
use ProjectBundle\Form\User\UserType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class RobotType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('serial_number')
            ->add('shell_color',ChoiceType::class,array(
                'choices' => Robot::getArrayShellColor()
            ))
            ->add('tray1Color',ChoiceType::class,array(
                'choices' => Robot::getTrayColor()
            ))
            ->add('tray2Color',ChoiceType::class,array(
                'choices' => Robot::getTrayColor()
            ))
            ->add('tray3Color',ChoiceType::class,array(
                'choices' => Robot::getTrayColor()
            ))
            ->add('hasBattery')
            ->add('volume')
            ->add('organisation',null,array('choices' => $options['data'][1]));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'ProjectBundle\Entity\Robot\Robot'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'projectbundle_robot_new';
    }


}

Here I need to get my variable $list, for the organisation field, so it's $options['data'][1].

But I have a mistake, which I understand but I do not know how to correct:

The form's view data is expected to be an instance of class ProjectBundle\Entity\Robot\Robot, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of ProjectBundle\Entity\Robot\Robot.

This is normal because now I pass it in an array, it's my variable $options['data'][0] that contains my object now.

How to do ? Thank you

1
  • Check EntityType and the parameter query_builder Commented Jun 7, 2017 at 15:37

1 Answer 1

1

You should pass ProjectBundle\Entity\Robot\Robot entity to the createForm method as a second parameter and an options array as a third parameter.

$form = $this->createForm('ProjectBundle\Form\Robot\RobotType', $entity, array($list));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.