2015-11-12 50 views
0

我在我的OneSitePage網站上嘗試製作簡單的聯繫表單時,坐在symfony2 FormBuilder下三個小時。我會注意到我主要是前端,但我需要通過跨越symfony2的Swiftmailer發送電子郵件。請不要問,爲什麼我使用symfony的:)Symfony2 - 聯繫FormBuilder。變量形式不存在於樹枝模板中。一個網站頁面

問題:我有呈現在我的主頁形式的問題,因爲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 = '[email protected]'; 

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

    } 

} 

現在的建設者,這是許多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'; 
    } 

而倒數第一路由和嫩枝:

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> 
[...] 

請支持,到處都是爲接觸不同的路由解決方案,我有一個網頁上的所有內容。歡迎提供所有提示,歡迎發表評論!

Uland

回答

4

你需要渲染的佈局樹枝表單:

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

} 

或者你也可以分開佈置,一個騙子troller是一種佈局:

控制器:

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

} 

}

而對於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> 
[..] 
+0

謝謝@了!你是老闆,真的!它正在工作。我認爲我現在可以從contactAction刪除createView,因爲它現在從indexAction加載。 –

2

這是因爲你沒有傳遞到您的視圖控制器創建的窗體對象,因爲你不打電話給你的聯繫人控制器。

如果它是一個頁面,創造一個名爲contact.html.twig與形式的樹枝視圖,並添加要在其中呈現的形式索引樹枝模板:

{{ render(controller('YodaHomeBundle:Home:contact')) }} 

這枝法會打電話給你indexControllercontactAction