Validation of a form before submission

二次信任 提交于 2019-12-02 03:55:33

In place of:

$form->submit($request->request->get($form->getName()));

Try:

$form->submit(array(), false);

You need to bind the the request to the form in order to fill the form with the submitted values, by using: $form->bind($request);

Here is a detailed explanation of what your code should look like:

//Create the form (you can directly use the method createForm() in your controller, it's a shortcut to $this->get('form.factory')->create() )
$form = $this->createForm(new MyEntityFormType, $myEntity, array('validation_groups' => 'my_validation_group'));

// Perform validation if post has been submitted (i.e. detection of HTTP POST method)
if($request->isMethod('POST')){

    // Bind the request to the form
    $form->bind($request);

    // Check if form is valid
    if($form->isValid()){

        // ... do your magic ...

    }

}

// Generate your page with the form inside
return $this->render('YourBundle:yourview.html.twig', array('form' => $form->createView() ) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!