symfony中的表单不存在

时间:2015-07-14 15:48:18

标签: php symfony

我正在测试一个与symfony联系的简单页面,但是当我想显示contact.html.twig时,它没有出现,我有错误表单不存在

有我的控制器

 public function sendAction()
{
   $contact = new Contact();

   $form = $this->createForm(new ContactType(),$contact);

   $_REQUEST = $this->getRequest();

   if($_REQUEST->isMethod('Post')){
       $form->bind($_REQUEST);

       if($form->isValid()){

           $contact = $form->getData();

           $em = $this->getDoctrine()->getManager();
           $em->persist($contact);
           $em.flush();

           return $this->redirect($this->generateUrl('front_office_send'));
       }
   }
    return $this->render('frontOfficeBundle:Contact:contact.html.twig', array('form' => $form->createView()));

}

和我的页面contact.html.twig:

{% extends"frontOfficeBundle::layoutheader.html.twig" %}
{% block container %}
<form id="form" class="form-light mt-20" role="form" method="post" action="{{ path('front_office_send') }}">

                        <div class="form-group">
                            <label>Nom</label>
                            {{form_widget(form.nom)}}

                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Adresse électronique &nbsp;</label>
                                    {{ form_widget(form.email) }}

                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label>Téléphone</label>
                                    {{ form_widget(form.tel) }}

                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label>Sujet &nbsp;</label>
                            {{ form_widget(form.sujet) }}

                        </div>
                        <div class="form-group">
                            <label>Message&nbsp;</label>
                            {{ form_widget(form.message) }}

                        </div>
                        <div class="row">
                            <div class="col-md-6">
                                <button type="reset" class="btn btn-light">Annuler</button>
                            </div>
                            <div class="col-md-6">

                                {{ form_widget(form._token) }}
                                <button  type="submit" class="btn btn-base btn-icon btn-icon-right btn-fly pull-right">
                                    <span>Envoyer message</span>
                                </button>
                            </div>
                        </div>
                    </form>

我有这个路由:

front_office_send:
 path:     /Contact
 defaults: { _controller: frontOfficeBundle:Contact:send }

错误从此行{{form_widget(form.nom)}}开始,变量形式不存在。

3 个答案:

答案 0 :(得分:1)

如果你把

{{ form_widget(form) }} 

它将显示整个表单,

如果你把

{{ form_widget(form.something) }} 

你的意思是场上的东西,所以你不能这样做,你必须在第一时间这样做。

如果您只想显示一个字段(就像我想要的那样......),您应该执行form_row

{{ form_row(form.nom) }}

这里有文档

http://symfony.com/doc/current/book/forms.html#rendering-a-form-in-a-template

答案 1 :(得分:0)

我的猜测是你的表单构建器不正确。我根据您在问题中未包含ContactType.php的事实做出此假设。您的form_widge必须与表单构建器中声明的字段一致。此外,如评论中所述,您的控制器中存在多个错误。

以下是我使用您提供的信息构建的快速测试页面的完整列表,它应该可以帮助您入门:

的routing.yml:

<?php
    namespace SCWDesignsBundle\Controller;

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use SCWDesignsBundle\Entity\Test;
    use SCWDesignsBundle\Form\TestType;

    class TestController extends Controller {
        public function testAction(Request $request) {
           $contact = new Test();
           $form = $this->createForm(new TestType(), $contact);
           $form->handleRequest($request);

           if($form->isValid()) {
            $em->persist($contact);
            $em->flush();

            return $this->redirect($this->generateUrl('front_office_send'));
          }

          return $this->render('SCWDesignsBundle:Test:test.html.twig', array('form' => $form->createView()));
        }
    }

控制器 - 显然更改名称空间:

<?php
    namespace SCWDesignsBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;

    /**
     * @ORM\Entity
     * @ORM\Table(name="test")
     */
    class Test {
        /**
         * @ORM\ID
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;

        /**
         * @ORM\Column(type="integer")
         */
        protected $nom;

        /**
         * @ORM\Column(type="string", columnDefinition="VARCHAR(9) NOT NULL")
         */
        protected $email;

        /**
         * @ORM\Column(type="string", columnDefinition="VARCHAR(9) NOT NULL")
         */
        protected $tel;

        /**
         * @ORM\Column(type="string", columnDefinition="VARCHAR(9) NOT NULL")
         */
        protected $sujet;

        /**
         * @ORM\Column(type="string", columnDefinition="VARCHAR(9) NOT NULL")
         */
        protected $message;
}

实体:

<?php
    namespace SCWDesignsBundle\Form;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;

    class TestType extends AbstractType {
        public function buildForm(FormBuilderInterface $builder, array $options) {
             $builder
                ->add('nom')
                ->add('email')
                ->add('tel')
                ->add('sujet')
                ->add('message');
        }

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

FormType:

{% block container %}
<form id="form" class="form-light mt-20" role="form" method="post" action="{{ path('front_office_send') }}">
        <div class="form-group">
            <label>Nom</label>
            {{form_widget(form.nom)}}

        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label>Adresse électronique &nbsp;</label>
                    {{ form_widget(form.email) }}

                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label>Téléphone</label>
                    {{ form_widget(form.tel) }}

                </div>
            </div>
        </div>
        <div class="form-group">
            <label>Sujet &nbsp;</label>
            {{ form_widget(form.sujet) }}

        </div>
        <div class="form-group">
            <label>Message&nbsp;</label>
            {{ form_widget(form.message) }}

        </div>
        <div class="row">
            <div class="col-md-6">
                <button type="reset" class="btn btn-light">Annuler</button>
            </div>
            <div class="col-md-6">

                {{ form_widget(form._token) }}
                <button  type="submit" class="btn btn-base btn-icon btn-icon-right btn-fly pull-right">
                    <span>Envoyer message</span>
                </button>
            </div>
        </div>
    </form>
{% endblock %}

查看页面:

Error:The project is using an unsupported version of Gradle

结果:

Results of page

答案 2 :(得分:0)

我的表单构建器是这样的:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('nom')
        ->add('email')
        ->add('tel')
        ->add('sujet')
        ->add('message')
    ;
}

我添加了requirements: _method: GET|POST 但总是表格不存在