symfony函数"form"不存在 [英] symfony The function "form" does not exist

查看:67
本文介绍了symfony函数"form"不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是symfony2的新手,我想创建一个简单的表单,这是我的代码: 在控制器中:

I am new in symfony2, I wanted to create a simple form and here is my code : in the controller :

    public function testAction(Request $request) {    
    $form = $this->createFormBuilder()
    ->add('name','text')
    ->add('age','integer')
    ->add('save','submit')
    ->getForm()
    ;//initializing the form
     return $this->render('LoginLoginBundle:Default:test.html.twig', array     ('myform'=>$form->createView()));
}  
}

在树枝文件中:

{%extends "::base.html.twig"%}
{% block body %}
{{ form(myform)}} 
{% endblock %}

我收到此错误: 无法加载类型提交" 即使删除-> add('save','submit'),我也会收到此错误:

I get this error : Could not load type "submit" and even if I delete ->add('save','submit') I get this error :

The function "form" does not exist. Did you mean "form_row", "form_rest", "form_label", "form_errors", "form_widget", "form_enctype" in LoginLoginBundle:Default:test.html.twig at line 3

推荐答案

我认为您拥有Symfony 2.2,应该将其更新为2.5. 但是,如果要保留版本,则必须阅读此 doc .他们告诉您您需要使用以下代码呈现表单:

I think you have Symfony 2.2 you should update it to 2.5. But if you want to keep your version, you must read this doc. They tell you you need to render your form with this code :

{% extends "::base.html.twig"%}
{% block body %}
    <form action="{{ path('your_route') }}" method="post" {{ form_enctype(myform) }}>
        {{ form_widget(myform) }}

        <input type="submit" />
    </form>
{% endblock %}

(您会看到提交"按钮必须在模板中呈现,并且您无法在使用Symfony版本的控制器中添加它)

(You can see that the "submit" button must be rendered in the template and that you can't add it in your controller with your version of Symfony)

这篇关于symfony函数"form"不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆