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}) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料