Symfony2和选项“;小部件”;不存在


Related forms in Symfony2 and The option "widget" does not exist

我有两个实体Profile和Cars。Profile有许多汽车。当我试图将CarsFormType嵌入ProfileFOrmType时,我得到了以下错误:

The form's view data is expected to be an instance of class Acme'UserBundle'Entity'Car, but is an instance of class Doctrine'Common'Collections'ArrayCollection. 

我试图找到解决方案,但没有看到任何有用的结果。我只想在创建配置文件时创建一辆车。

我的型号代码:

简介:

/**
 * @ORM'OneToMany(targetEntity="Car", mappedBy="profile", cascade={"persist"})
 */
private $cars;

ProfileFormType:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('fio', null, array('label' => 'ФИО'));
    $builder->add('birthDate', null, array('label' => 'Дата рождения', 'widget' => 'single_text'));
    $builder->add('file', 'file', array('label' => 'Фото', 'required' => false));
    $builder->add('telephone', null, array('label' => 'Телефон'));
    $builder->add('contactMail', null, array('label' => 'Контактная почта'));
    /* $builder->add('category', 'choice', array(
        'label' => 'Категория прав',
        'choices' => array('B' => 'Категория B', 'C' => 'Категория C',),
        'expanded' => true,
        'multiple' => true,
    ));*/
        $builder->add('driverLicence', null, array('label' => 'Лицензия', 'widget' => 'single_text', 'required' => false));
        $builder->add('yearOnRoad', null, array('label' => 'Год за рулем', 'required' => false));
        $builder->add('cars', new CarType(), array('label' => 'Авто', 'required' => false));
//        $builder->add('cars', 'entity', array('label' => 'Авто', 'required' => false,      'class' => 'VputiUserBundle:Car'));
//        $builder->add('cars', 'collection', array('label' => 'Авто', 'required' =>     false, 'type' => new CarType()));
    $builder->add('submit', 'submit');
}
/**
 * {@inheritDoc}
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class'         => 'Vputi'UserBundle'Entity'Profile',
        'cascade_validation' => true,
    ));
}
/**
 * {@inheritDoc}
 */
public function getName()
{
    return 'profile_driver';
}

当在车内形成类型i raplece这个:

 public function setDefaultOptions(OptionsResolverInterface $resolver)
 {
    $resolver->setDefaults(array(
        'data_class' => 'Acme'UserBundle'Entity'Car',
        'cascade_validation' => true,
    ));
 }

到此:

 public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => null,
        'cascade_validation' => true,
    ));
}

我收到新错误:

The option "widget" does not exist. Known options are: "action", "attr", "auto_initialize", "block_name", "by_reference", "cascade_validation", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled",

等等。我的问题在哪里?

我只能假设profile和car之间的关系是oneToMany,这意味着在您的表单中,您需要使用CarTypes的集合,而不是单个cars字段。这意味着,当表单请求它正在获取的cars变量和Car对象的ArrayCollection时,而不是表单期望的单个Car

更改。。

$builder->add('cars', new CarType(), array('label' => 'Авто', 'required' => false));

收件人。。

$builder->add('cars', 'collection', array(
    'label'    => 'Авто',
    'required' => false,
    'type'     => new CarType(),
));

您可以阅读更多关于在文档中嵌入集合的信息,集合类型的实际选项在这里。

您想要实现的是拥有一个表单集合

我认为正确的设置方法是使用这种方法:

 $buider->add('cars', 'collection', array(
            'type' => new carType(),
            'allow_add' => true,
            'allow_delete' => true,
        )
    );

您没有设置带有表单字段的数组。你在auto中需要的字段需要进入carType。关于错误:Symfony期望Acme''UserBundle''Entity''Car实体与相应的类型匹配,而它却收到了另一个东西。这就是为什么我试图为你指明正确的方向。

请在此处查看一对多表单示例及其实体。

就在完成扑救后,我看到Qoop与我的答案完全一致:)所以问问你是否无法让它运行。