1

我的 buildForm 类如下所示。

    $builder->add('icon','entity',
            array(
            'class' => 'UserBundle:IconPics',
            'property' => 'label', // .. or whatever property the image location is stored.
            'expanded' => true,
            'multiple' => false,        
            'label' => 'form.icon', 'translation_domain' => 'FOSUserBundle',
             'query_builder' => function ($repository) {
                return $repository->createQueryBuilder('i')
                ->add('where', 'i.enabled = true');
             }
    ));

如何设置此单选按钮的默认值?

根据彼得贝利的回答

use Acme\UserBundle\Entity\IconPics;
//
$IconPics = new IconPics();
// howw can I select the target Icon?????
4

1 回答 1

6

您可以通过几种方式设置表单的初始数据

  1. 使用数据选项

    'data' => $default
    

    $default的实例在哪里UserBundle\Entity\IconPics

  2. 提供初始数据源

    // Assuming form is created in a controller
    $data = new WhateverYourEntitiyIs();
    $defaultIcon = new \UserBundle\Entity\IconPics();
    // Set properties on $defaultIcon here
    // or select $defaultIcon via the repository
    
    $data->setIcon( $defaultIcon );
    $form = $this->createForm( 'form_type', $data );
    
于 2013-11-13T03:49:25.180 回答