duansai1314 2017-08-23 15:20
浏览 21
已采纳

在Symfony中使用form_end会导致内部服务器错误

I recently cleaned my Twig templates in order to avoid deprecated calls on Symfony 2.8 and I got a strange behaviour.

This is how my template looks like :

{{ form_start(form, {'attr': {'class': 'form_template'} }) }}
    {{ form_widget(form) }}
    <input type="submit" value="Send"/>
{{ form_end(form) }}

This is how my form looks like :

class MyFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', 'text', array('label' => 'your email'));
    }

    public function getName()
    {
        return 'some_fancy_name';
    }
}

And this is how my controller looks like

public function someAction(Request $request)
{
    $form = $this->createForm(new MyFormType(), null);

    if ($request->getMethod() == 'POST') 
    {
        // do something with the form contents
        return $this->redirect($this->generateUrl('somewhere'));
    }
    return $this->render('::myform.html.twig', array(
        'form' => $form->createView(),
    ));
}

When rendering the template, everything is working fine until Twig renders the form_end() function and then throws an Error: Maximum function nesting level of '100' reached, aborting! (500 Internal Server Error)

I tried increasing the xdebug.max_nesting_level setting in my PHP configuration but it did not help, the only fix that I found which resolves the issue is :

{{ form_start(form, {'attr': {'class': 'form_template'} }) }}
    {{ form_widget(form) }}
    <input type="submit" value="Send"/>
    {{ form_rest(form) }}
</form>

Which is an ugly fix.

Now I am wondering why am I getting such error with form_end(form) while everything works fine with form_rest(form) ?

  • 写回答

1条回答 默认 最新

  • dounuo9921 2017-08-24 10:36
    关注

    The form_widget(form) renders the whole form recursively as indicated in the documentation on form collections and causes the serveur to exceed maximum nesting level. Rendering every field manually fixed the issue.

    Moreover, calling form_end(form) calls form_rest(form), so when this is not necessary, I had to manually disable the rest rendering to fully work this out, according to documentation on form functions and variables.

    I had to change my Twig template to the following:

    {{ form_start(form, {'attr': {'class': 'form_template'} }) }}
        {{ form_row(form.email) }}
        <br/>
        <input type="submit" value="Send"/>
    {{ form_end(form, {'render_rest': false}) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 初学者用plt报错,求解答
  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?