9

我正在尝试在实体字段中设置选定的值。根据我看到的有关此主题的许多讨论,我尝试设置该data选项,但默认情况下不会选择任何值:

class EventType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('place', 'entity', array(
                'class' => 'RoyalMovePhotoBundle:Place',
                'property' => 'name',
                'empty_value' => "Choisissez un club",
                'mapped' => false,
                'property_path' => false,
                'data' => 2
            ))
            ->add('begin')
            ->add('end')
            ->add('title')
            ->add('description')
        ;
    }

    // ...
}

通过寻找更多,我发现有些人不得不停用到实体的表单映射。这似乎合乎逻辑,所以我尝试添加'mapped' => false选项,但没有成功......

如果有帮助,这是我的控制器:

class EventController extends Controller
{
    // ...

    public function addAction()
    {
        $request = $this->getRequest();
        $em = $this->getDoctrine()->getManager();

        $event = new Event();
        $form = $this->createForm(new EventType(), $event);

        $formHandler = new EventHandler($form, $request, $em);

        if($formHandler->process()) {
            $this->get('session')->getFlashBag()->add('success', "L'évènement a bien été ajouté.");
            return $this->redirect($this->generateUrl('photo_event_list'));
        }

        return $this->render('RoyalMovePhotoBundle:Event:add.html.twig', array(
            'form' => $form->createView()
        ));
    }
}

EventHandler班级:

class EventHandler extends AbstractHandler
{
    public function process()
    {
        $form = $this->form;
        $request = $this->request;

        if($request->isMethod('POST')) {
            $form->bind($request);

            if($form->isValid()) {
                $this->onSuccess($form->getData());
                return true;
            }
        }

        return false;
    }

    public function onSuccess($entity)
    {
        $em = $this->em;

        $em->persist($entity);
        $em->flush();
    }
}

我现在有点卡住了,有人有想法吗?

4

4 回答 4

25

您只需要设置字段的数据:

    
    class EventController extends Controller
    {
        // ...

        public function addAction()
        {
           $request = $this->getRequest();
            $em = $this->getDoctrine()->getManager();

            $event = new Event();
            $form = $this->createForm(new EventType(), $event);

            // -------------------------------------------
            // Suppose you have a place entity..
            $form->get('place')->setData($place);
            // That's all..
            // -------------------------------------------

            $formHandler = new EventHandler($form, $request, $em);

            if($formHandler->process()) {
                $this->get('session')->getFlashBag()->add('success', "L'évènement a bien été ajouté.");
                return $this->redirect($this->generateUrl('photo_event_list'));
            }

            return $this->render('RoyalMovePhotoBundle:Event:add.html.twig', array(
                'form' => $form->createView()
            ));
        }
    }
    

于 2013-09-13T17:47:57.087 回答
7

为了使选项出现在表单中,您应该为实体本身设置相应的值。

$place = $repository->find(2);
$entity->setPlace($place);
$form = $this->createForm(new SomeFormType(), $entity);
....
于 2013-04-01T21:30:32.487 回答
1

对于non-mapped实体选择字段,我发现最简单的方法是使用choice_attr带有callable. 这将遍历选择的集合,并允许您根据您的条件添加自定义属性,并使用扩展、多个和自定义属性选项。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('place', 'entity', array(
            //...
            'choice_attr' => function($place) {
                $attr = [];
                if ($place->getId() === 2) {
                    $attr['selected'] = 'selected';
                    //for expanded use $attr['checked'] = 'checked';
                 }
                 return $attr;
            }
       ))
       //...
    ;
}
于 2017-09-08T19:46:24.217 回答
0

当您使用该query_builder选项时,该data选项需要一个集合实例,并且您不想通过仅添加某些字段来触摸您的控制器setDatas,并且您已经在表单类型类中拥有查询构建器和重新填充选项的 ID,您可以重新填充选择,如下所示:

// Querybuilder instance with filtered selectable options
$entities = $qb_all; 
// Querybuilder instance filtered by repopulating options (those that must be marked as selected)
$entities_selected = $qb_filtered; 

然后在你的 add() 方法中

'data' => $entities_selected->getQuery()->getResult(), // Repopulation
'query_builder' => $entities,

编辑:真实用例示例

您想重新填充使用以下元素呈现的复选框组:

Label: What is your favourite meal?

4 Checkboxes: Pasta, Pizza, Spaghetti, Steak

并且您想重新填充 2 个复选框:

Pizza, Steak

$qb_all将是具有所有 4 个可选复选框的 QueryBuilder 实例

$qb_filtered将是一个带有重新填充 Checkboxes 的新的附加 QueryBuilder 实例Pizza, Steak。所以是前一个的“过滤”版本。

于 2015-11-24T11:27:50.123 回答