[英]Symfony2 - contact FormBuilder. Variable form does not exist in twig template. One site page
當我嘗試在OneSitePage網站上制作一個簡單的聯系表單時,我坐在symfony2 FormBuilder下三個小時。 我會注意到我主要是前端,但是我需要通過symfony2通過Swiftmailer發送電子郵件。 請不要問,為什么我要使用symfony :)
問題:我的主頁上有渲染表單的問題,因為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()
);
}
}
現在的構建器,用於許多tut的簡單構建器。
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';
}
對於最后的路由和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>
[...]
請尋求支持,到處都是用於不同聯系方式的解決方案,我將所有內容都放在一頁上。 歡迎所有提示,請發表評論!
烏蘭
您需要通過以下方式在布局樹枝上呈現表單:
public function indexAction(){
$form = $this->createForm(new ContactType());
return $this->render('YodaHomeBundle::layout.html.twig',array('form' => $form->createView());
}
也可以拆分布局,一個控制器就是一種布局:
控制器:
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>
[..]
這是因為您沒有傳遞視圖到您在控制器中創建的表單對象,因為您沒有調用聯系人控制器。
如果是一頁,請使用您的表單創建一個名為contact.html.twig
的樹枝視圖,並在您想要顯示表單的位置添加索引樹枝模板:
{{ render(controller('YodaHomeBundle:Home:contact')) }}
這個樹枝方法將調用您的indexController
和contactAction
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.