简体   繁体   English

Symfony2:形式:绑定数据

[英]Symfony2: Form:bind data

I've got simple form 我有简单的表格

    $transformer = new ModuleToArrayTransformer($this->entityManager);
    $builder->add('modules', 'collection', array(
        'type' => 'checkbox',
        'required' => false,
        'data' => $options['data'][0]->getModules(),
    ))
    ->add('save', 'submit', array(
        'label' => 'btn_update_and_edit_again',
        'attr' => array(
            'class' => 'btn btn-primary'
        )
    ))
    ->addViewTransformer($transformer);

And controller for it: 和它的控制器:

  $em = $this->getDoctrine()->getManager(); 
    $user = $this->get('security.context')->getToken()->getUser();

    $module = $em->getRepository('AcmeProjectBundle:Module')
        ->findBy(
            array(
                'user' => $userId,
                'project' => $projectId,
            )
        );

    if (!$module) {
        $module = $this->createAction($projectId, $userId);
    }

    $userData = $em->getRepository('AcmeSonataUserBundle:User')->find($userId);
    $projectData = $em->getRepository('AcmeProjectBundle:Project')->find($projectId);
    $form = $this->createForm(new ModuleType($em), $module, array('user' => $userData, 'project' => $projectData));
    if ($request->getMethod() == 'GET') {
        if ($form->isValid()) {
        }

    }
    return $this->render('AcmeProjectBundle:Module:edit.html.twig', 
        array(
            'form' => $form->createView(),
            'projectId' => $projectId,
            'userId' => $userId,
        )
    );

The form is connected with another form. 该表格与另一表格相连。 When I click on checkbox in the first form this form is generated - correctly. 当我单击第一个表单中的复选框时,将正确生成此表单。 Until I try to $form->bind($request); 直到我尝试$form->bind($request);

When I try to bind request it is showing sth like "This value is incorrect" 当我尝试绑定请求时,它显示诸如“此值不正确”之类的信息

The $_GET seems to be fine $_GET似乎很好

 array (size=1) 'acme_projectbundle_module' => array (size=2) 'modules' => array (size=8) 'stats' => string '1' (length=1) 'time' => string '1' (length=1) 'issues' => string '1' (length=1) 'metrics' => string '1' (length=1) 'timeLine' => string '1' (length=1) 'information' => string '1' (length=1) 'status' => string '1' (length=1) 'risks' => string '1' (length=1) 'save' => string '' (length=0) 

as well as the $request variable 以及$request变量

If form looks exactly like my $_GET request what's the problem? 如果表单看起来完全像我的$_GET请求,那是什么问题? As far as I know $_GET should contain the same values as $request->parameters field. 据我所知,$ _ GET应该包含与$request->parameters字段相同的值。 How is is supposed to look like then? 那应该是什么样子? Where is the problem? 问题出在哪儿?

You are trying to bind the form and check isValid when the request method is GET ie in the case when you just want to display the form to the user. 您正在尝试绑定表单,并在请求方法为GET时检查isValid,即仅向用户显示表单时。 And in that case you are bound to get the error 在这种情况下,您一定会遇到错误

"This value is incorrect" “此值不正确”

because the $request does not contain any values. 因为$request不包含任何值。

So you should bind the form only when the data is submitted and not when the form is being displayed. 因此,仅在提交数据时才应绑定表单,而在显示表单时则不应绑定表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM