Symfony 3 FormType - 通过选项数组的实体
Symfony 3 FormType - Entity through options array
我想知道如何在我的 createForm 函数中正确传递我的实体和另一个变量?
让我解释一下,我想这样做:
$repo = $em->getRepository('ProjectBundle:Organisation\Organisation');
$list = $repo->children($this->getUser()->getOrganisation());
$form = $this->createForm('ProjectBundle\Form\Robot\RobotType', array($entity,$list));
我需要向我的 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';
}
}
这里我需要为组织字段获取变量 $list,所以它是 $options['data'][1]
。
但我有一个错误,我明白了,但我不知道如何更正:
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.
这是正常的,因为现在我将它传递到一个数组中,它是我的变量 $options['data'][0]
现在包含我的对象。
怎么办?
谢谢
您应该将 ProjectBundle\Entity\Robot\Robot 实体作为第二个参数传递给 createForm 方法,并将选项数组作为第三个参数。
$form = $this->createForm('ProjectBundle\Form\Robot\RobotType', $entity, array($list));
我想知道如何在我的 createForm 函数中正确传递我的实体和另一个变量?
让我解释一下,我想这样做:
$repo = $em->getRepository('ProjectBundle:Organisation\Organisation');
$list = $repo->children($this->getUser()->getOrganisation());
$form = $this->createForm('ProjectBundle\Form\Robot\RobotType', array($entity,$list));
我需要向我的 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';
}
}
这里我需要为组织字段获取变量 $list,所以它是 $options['data'][1]
。
但我有一个错误,我明白了,但我不知道如何更正:
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.
这是正常的,因为现在我将它传递到一个数组中,它是我的变量 $options['data'][0]
现在包含我的对象。
怎么办? 谢谢
您应该将 ProjectBundle\Entity\Robot\Robot 实体作为第二个参数传递给 createForm 方法,并将选项数组作为第三个参数。
$form = $this->createForm('ProjectBundle\Form\Robot\RobotType', $entity, array($list));