Symfony2 - 联系FormBuilder.树枝模板中不存在变量形式.一个网站页面

Ula*_*oof 0 php forms symfony twig

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

问题:我的homePage上有渲染表单的问题,因为Symfony说,就像在主题:

"变量"形式"在YodaHomeBundle :: layout.html.twig中不存在..." 并且它指向我使用树枝形状的行(在TWIG部分下面附上)

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

首先是控制器,我有两个动作,索引和联系人.

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()
        );

    }

}
Run Code Online (Sandbox Code Playgroud)

现在构建器,简单的构建器,用于许多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';
    }
Run Code Online (Sandbox Code Playgroud)

对于最后的位置路由和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>
[...]
Run Code Online (Sandbox Code Playgroud)

请求支持,无处不在是不同联系路线的解决方案,我在一页上有所有内容.欢迎提供所有提示,请征求意见!

Uland

HAD*_*HAD 5

你需要在布局树枝上渲染你的表格:

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

}
Run Code Online (Sandbox Code Playgroud)

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

控制器:

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());

}
Run Code Online (Sandbox Code Playgroud)

}

对于TWIG:layout.html.twig:

[..]
<div>{{ render(controller('YodaHomeBundle:Home:contact')) }}</div>
[..]
Run Code Online (Sandbox Code Playgroud)

contactlayout.html.twig:

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