Php Symfony2-联系FormBuilder。细枝模板中不存在变量形式。一个站点页面
我在symfony2 FormBuilder下坐了三个小时,试图在我的OneSitePage网站上制作一个简单的联系表单。我会注意到,我主要是前端,但我需要通过Swiftmailer通过symfony2发送电子邮件。请不要问,我为什么使用symfony:) 问题:我对我的主页上的呈现形式有意见,因为Symfony说,就像在主题中一样: YodaHomeBundle::layout.html.twig…”中不存在“变量”形式“ 它是我使用细枝形式的线的点(附在下面的细枝部分) 好的,这是介绍。下面我展示了controller的PHP类和ContactType类,下面我还附带了layout.html.twig文件 首先是控制器,我有两个动作,索引和联系Php Symfony2-联系FormBuilder。细枝模板中不存在变量形式。一个站点页面,php,forms,symfony,twig,Php,Forms,Symfony,Twig,我在symfony2 FormBuilder下坐了三个小时,试图在我的OneSitePage网站上制作一个简单的联系表单。我会注意到,我主要是前端,但我需要通过Swiftmailer通过symfony2发送电子邮件。请不要问,我为什么使用symfony:) 问题:我对我的主页上的呈现形式有意见,因为Symfony说,就像在主题中一样: YodaHomeBundle::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()
);
}
}
现在是builder,简单的builder,用于许多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';
}
对于最后一位布线和细枝:
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>
[...]
mail\u创建:
路径:/主页
默认值:{u控制器:“YodaHomeBundle:Home:contact”}
要求:{u方法:post}
[...]
{{form_start(form)}}
{{form_widget(form)}
{{form_end(form)}}
[...]
请支持,到处都是不同的联系路线的解决方案,我在一个页面上的一切。欢迎所有提示,请发表意见
Uland这是因为您不调用联系人控制器,所以不会将在控制器中创建的表单对象传递给视图 如果是一页,请使用表单创建名为
contact.html.twig
的细枝视图,并将索引细枝模板添加到要呈现表单的位置:
{{ render(controller('YodaHomeBundle:Home:contact')) }}
此细枝方法将调用您的
indexController
和contactAction
,您需要通过以下方式在布局细枝上呈现表单:
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());
}
}
至于细枝:
layout.html.twig:
[..]
<div>{{ render(controller('YodaHomeBundle:Home:contact')) }}</div>
[..]
[..]
<form action="{{ path('mail_create') }}" method="post">
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</form>
[..]
[…]
{render(controller('YodaHomeBundle:Home:contact'))}
[..]
contactlayout.html.twig:
[..]
<div>{{ render(controller('YodaHomeBundle:Home:contact')) }}</div>
[..]
[..]
<form action="{{ path('mail_create') }}" method="post">
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
</form>
[..]
[…]
{{form_start(form)}}
{{form_widget(form)}
{{form_end(form)}}
[..]
谢谢@HAD!你是老板,真的!它现在正在工作。我认为现在可以从CelcTcAct删除CytVeVIEW,因为它现在从索引动作加载。