Symfony2 - 联系FormBuilder。变量形式不存在于树枝模板中。一个网站页面 [英] Symfony2 - contact FormBuilder. Variable form does not exist in twig template. One site page

查看:154
本文介绍了Symfony2 - 联系FormBuilder。变量形式不存在于树枝模板中。一个网站页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在OneSitePage网站上制作一个简单的联系表单时,我坐在symfony2 FormBuilder下三个小时。我会注意到我主要是前端,但我需要通过跨越symfony2的Swiftmailer发送电子邮件。请不要问,为什么我使用的是symfony:)

问题:我的主页有渲染表单问题,因为Symfony说,就像在主题中一样:



变量表单在YodaHomeBundle :: layout.html.twig中不存在...
,它指向行我正在使用树枝形式(在TWIG部分中附上)



好的,那是介绍。下面我介绍PHP类的控制器和ContactType类,下面还附加了layout.html.twig文件。



首先来控制器,我有两个动作,索引和联系。

  namespace Yoda\HomeBundle\Controller; 

使用Symfony \ Bundle \ FrameworkBundle \Controller\Controller;
使用Symfony \Component\HttpFoundation\Request;
使用Sensio \ Bundle \FrameworkExtraBundle\Configuration\Template;
使用Symfony \Component\Routing\Annotation\Route;
使用Yoda \UserBundle \Entity \User;
使用Yoda \ HomeBundle \ Form \ContactType;
使用Symfony \Component\Form\FormInterface;


class HomeController extends Controller {
$ b $ / **
* @Route(/ home,name =homePage)
* @Template()
*
* /
public function indexAction(){

return $ this-> render('YodaHomeBundle :: layout.html 。枝条');


$ b public function contactAction(Request $ request)
{

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

$ adress ='grzegorz.developer@gmail.com';

if($ request-> isMethod('POST'))
{
$ form-> submit($ request-> request-> get($形状配合>的getName()));

if($ form-> isValid())
{
$ message = \Swift_Message :: newInstance()
- > setSubject($ form - > get('subject') - > getData())
- > setFrom($ form-> get('email') - > getData())
- > setTo($ adress)
- > setBody(
$ this-> renderView('@ YodaHome / mail / contact.html.twig',
array(
'ip '=> $ request-> getClientIp(),
'name'=> $ form-> get('name') - > getData(),
'message'=> ; $ form-> get('message') - > getData()
))
);

$ this-> get('mailer') - > send($ message);

$ request-> getSession() - > getFlashBag() - > add('成功,您的邮件已发送!谢谢,我会尽快回复您\\'可能!');

返回$ this->重定向($ this-> generateUrl('homePage'));



$ b return array(
'form'=> $ form-> createView()
);

}

}

  class ContactType extends AbstractType 
{

public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder
- > add('name','text',array(
'attr'= >数组(
'placeholder'=&';'What's your name?',
'length'=>'。{2,}'


- > add('email','email',array(
'attr'=> array(
'placeholder'=>'所以我可以写回来(

))
- > add('subject','text',array(
'attr'=> array(
' placeholder'=>'消息的主题',
'patt ($'$'$&'; $'$'$'$'$'$' >数组(
'cols'=>'90',
'row'=>'10',
'placeholder'=>'将您的消息广告给我... '

));


public function setDefaultOptions(OptionsResolverInterface $ resolver)
{
$ collectionConstraint = new Collection(array(
'name'=> array(
new NotBlank(array('message'=&';'你忘了名字')),
new Length(array('min'=> 2))
),
'email'=> array(
new NotBlank(array('message'=&';'Email should not be blank。')),
new Email(array('message' =)'无效的电子邮件地址'))
),
'subject'=>数组(
new NotBlank(array('message'=&''主题不应该为空')),
new Length(array('min'=> 3))
),
'message'=>数组(
new NotBlank(array( 'message'=>'消息不应该为空')),
新的长度(数组('min'=> 5))

));

$ resolver-> setDefaults(array(
'constraints'=> $ collectionConstraint
));
}

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

以及上次路线和TWIG:

  mail_create:
路径:/ homePage
默认值:{_controller:YodaHomeBundle:Home:contact}
requirements: _method:post}

[...]
{{form_start(form)}}
{{form_widget(form))}
{{form_end(form)}}
< / form>
[...]

请支持,无论何地, ,并且我拥有一个页面上的所有内容。所有提示,欢迎您提出意见!



Uland

解决方案

你需要在layout twig上渲染你的表单:

  public function indexAction(){
$ form = $ this- > createForm(new ContactType());
return $ this-> render('YodaHomeBundle :: layout.html.twig',array('form'=> $ form-> createView());

}

或者你可以拆分布局,一个控制器是一个布局:

Controller:

  class HomeController extends Controller {

/ **
* @Route(/ home,name =homePage)
* @Template()
*
* /
public function indexAction(){

return $ this-> render('YodaHomeBundle :: layout.html.twig');

}

public function contactAction(Request $ request)
{

$ form = $ this-> createForm(new ContactType());
//做你的代码

return array(
'YodaHomeBundle :: contactlayout.html.twig',
array('form'=> $ form-> createView());

}

}



以及TWIG:
布局。 html.twig:


  [..] 
< div> { {render(controller('YodaHomeBundle:Home:contact'))}}< / div>
[..]

contactlayout.html.twig:

  [..] 
{{form_start(form)}}
{{form_widget(form))}
{{form_end(form)}}
< / form>
[..]


I am sitting three hours under symfony2 FormBuilder, when I try to make a simple contact form, on my OneSitePage website. I will notice that I am mostly frontend, but I need send emails via Swiftmailer across symfony2. Please do not ask, why I am using symfony:)

PROBLEM: I have issue with render form on my homePage, because Symfony says, like in subject:

"Variable "form" does not exist in YodaHomeBundle::layout.html.twig..." and it point for line where I am useing twig form(attached below in TWIG section)

Ok, that was introduction. Below I present PHP class of controller and ContactType class, also below I had attached layout.html.twig file.

First comes controller, where I have two actions, index and contact.

namespace Yoda\HomeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Yoda\UserBundle\Entity\User;
use Yoda\HomeBundle\Form\ContactType;
use Symfony\Component\Form\FormInterface;


class HomeController extends Controller{

    /**
      * @Route("/home", name="homePage")
      * @Template()
      *
      */
    public function indexAction(){

        return $this->render('YodaHomeBundle::layout.html.twig');

    }

    public function contactAction(Request $request)
    {

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

        $adress = 'grzegorz.developer@gmail.com';

        if($request->isMethod('POST'))
        {
            $form->submit($request->request->get($form->getName()));

            if($form->isValid())
            {
                $message = \Swift_Message::newInstance()
                    ->setSubject($form->get('subject')->getData())
                    ->setFrom($form->get('email')->getData())
                    ->setTo($adress)
                    ->setBody(
                        $this->renderView('@YodaHome/mail/contact.html.twig',
                            array(
                                'ip'        =>  $request->getClientIp(),
                                'name'      =>  $form->get('name')->getData(),
                                'message'   =>  $form->get('message')->getData()
                            ))
                    );

                $this->get('mailer')->send($message);

                $request->getSession()->getFlashBag()->add('Success, your mail has been send! Thank you, I will back to you, as soon as it\'s possible!');

                return $this->redirect($this->generateUrl('homePage'));

            }
        }

        return array(
            'form' => $form->createView()
        );

    }

}

now builder, Simple builder which is used on many tuts.

class ContactType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', 'text', array(
            'attr' => array(
                'placeholder'   => 'What\'s your name?',
                'length'        => '.{2,}'
            )
        ))
        ->add('email', 'email', array(
            'attr' => array(
                'placeholder'   => 'So I can write back to you'
            )
        ))
        ->add('subject', 'text', array(
            'attr' => array(
                'placeholder'   => 'Subject of your message',
                'pattern'       => '.{5,}'
            )
        ))
        ->add('message', 'text', array(
            'attr' => array(
                'cols'          => '90',
                'row'           => '10',
                'placeholder'   => 'And ad your message to me...'
            )
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $collectionConstraint = new Collection(array(
            'name' => array(
                new NotBlank(array('message' => 'You forgot about the Name.')),
                new Length(array('min' => 2))
            ),
            'email' => array(
                new NotBlank(array('message' => 'Email should not be blank.')),
                new Email(array('message' => 'Invalid email address.'))
            ),
            'subject' => array(
                new NotBlank(array('message' => 'Subject should not be blank.')),
                new Length(array('min' => 3))
            ),
            'message' => array(
                new NotBlank(array('message' => 'Message should not be blank.')),
                new Length(array('min' => 5))
            )
        ));

        $resolver->setDefaults(array(
            'constraints' => $collectionConstraint
        ));
    }

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

And for last place routing and TWIG:

mail_create:
    path:     /homePage
    defaults: { _controller: "YodaHomeBundle:Home:contact" }
    requirements: { _method: post }

[...]
    <form action="{{ path('mail_create') }}" method="post">
                    {{ form_start(form) }}
                    {{ form_widget(form) }}
                    {{ form_end(form) }}
    </form>
[...]

Please for support, everywhere are solutions for different routes for contact, and I have everything on one page. All hints are welcome, please for comments!

Uland

解决方案

you need render your form on layout twig by:

 public function indexAction(){
    $form = $this->createForm(new ContactType());
    return $this->render('YodaHomeBundle::layout.html.twig',array('form' => $form->createView());

}

Or you can split layout, one controller is one layout:

Controller:

class HomeController extends Controller{

/**
  * @Route("/home", name="homePage")
  * @Template()
  *
  */
public function indexAction(){

    return $this->render('YodaHomeBundle::layout.html.twig');

}

public function contactAction(Request $request)
{

    $form = $this->createForm(new ContactType());
    // do your code

    return array(
        'YodaHomeBundle::contactlayout.html.twig',
    array('form' => $form->createView());

}

}

And for TWIG: layout.html.twig:

[..]
<div>{{ render(controller('YodaHomeBundle:Home:contact')) }}</div>
[..]

contactlayout.html.twig:

[..]
    <form action="{{ path('mail_create') }}" method="post">
                {{ form_start(form) }}
                {{ form_widget(form) }}
                {{ form_end(form) }}
    </form>
[..]

这篇关于Symfony2 - 联系FormBuilder。变量形式不存在于树枝模板中。一个网站页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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