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 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上
  • ¥20 100元python和数据科学实验项目
  • ¥15 根据时间在调用出列表
  • ¥15 R 包chipseeker 安装失败
  • ¥15 Veeam Backup & Replication 9.5 还原问题
  • ¥15 vue-print-nb
  • ¥15 winfrom的datagridview下拉框变成了黑色,渲染不成功
  • ¥20 利用ntfy实现短信推送