ねえ、私はこの symfony2 フレームワークに慣れていないので、いくつかのヘリが必要です。
これはお問い合わせフォームのコードです。フォームをビュー ページにレンダリングしようとすると、エラーが発生します。次のコードとエラーも参照してください。
誰かが問題の原因を知っている場合は、私に知らせてください..
ありがとう!
ContactType.php
<?php
// src/Aleksandar/IntelMarketingBundle/Resources/views/ContactType.php
namespace Aleksandar\IntelMarketingBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'attr' => array(
'placeholder' => 'What\'s your name?',
'pattern' => '.{2,}' //minlength
)
))
->add('email', 'email', array(
'attr' => array(
'placeholder' => 'So I can get back to you.'
)
))
->add('subject', 'text', array(
'attr' => array(
'placeholder' => 'The subject of your message.',
'pattern' => '.{3,}' //minlength
)
))
->add('message', 'textarea', array(
'attr' => array(
'cols' => 20,
'rows' => 2,
'placeholder' => 'And your message to me...'
)
));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$collectionConstraint = new Collection(array(
'name' => array(
new NotBlank(array('message' => 'Name should not be blank.')),
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 'contact';
}
}
?>
コントローラー
<?php
namespace Aleksandar\IntelMarketingBundle\Controller;
use Aleksandar\IntelMarketingBundle\Form\Type\ContactType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class DefaultController extends Controller
{
/**
* @Route("contact", _name="contact")
* @Template()
*/
public function contactAction()
{
return $this->render('AleksandarIntelMarketingBundle::contact.html.php');
$form = $this->createForm(new ContactType());
if ($request->isMethod('POST')) {
$form->bind($request);
if ($form->isValid()) {
$message = \Swift_Message::newInstance()
->setSubject($form->get('subject')->getData())
->setFrom($form->get('email')->getData())
->setTo('info@intelmarketing.es')
->setBody(
$this->renderView(
'AleksandarIntelMarketingBundle::contact.html.php',
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 email has been sent! Thanks!');
return $this->redirect($this->generateUrl('contact'));
}
}
return array(
'form' => $form->createView()
);
}
}
ルーティング
aleksandar_intel_marketing_contactpage:
pattern: /contact
defaults: { _controller: AleksandarIntelMarketingBundle:Default:contact }
ビューフォルダーにあるcontact.html.php
<?php echo $view['form']->form($form) ?>
上記のコードをcontact.html.phpに追加すると、次のようになりました
Notice: Undefined variable: form in C:\wamp\www\symfony\src\Aleksandar\IntelMarketingBundle\Resources\views\contact.html.php line 1
500 Internal Server Error - ContextErrorException
私の見解の規範
<!-- app/Resources/views/contact.html.php -->
<!DOCTYPE html>
<html>
<head><?php echo $view->render('AleksandarIntelMarketingBundle:template:head.html.php') ?></head>
<body>
<div class="wrapper">
<div id="header"><?php echo $view->render('AleksandarIntelMarketingBundle:template:header.html.php') ?></div>
<div id="leftside"><?php echo $view->render('AleksandarIntelMarketingBundle:template:leftside.html.php') ?></div>
<div id="rightside"><?php echo $view->render('AleksandarIntelMarketingBundle:template:rightside.html.php') ?>
<div class="container">
</div>
<script type="text/javascript">
$(document).ready(function () {
if (!$.browser.webkit) {
$('.container').jScrollPane();
}
});
</script>
<div id="clear"></div>
</div>
<div id="footer">
<?php echo $view->render('AleksandarIntelMarketingBundle:template:footer.html.php') ?>
</div>
</div>
</body>
</html>