渲染到视图页面时出现symfony2联系表单错误

时间:2013-08-07 17:07:10

标签: symfony

嘿,我是这个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 }

以及view文件夹

上的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>

1 个答案:

答案 0 :(得分:0)

您需要将form添加到此块:

$this->renderView('AleksandarIntelMarketingBundle::contact.html.php', array(
    'ip'      => $request->getClientIp(),
    'name'    => $form->get('name')->getData(),
    'message' => $form->get('message')->getData(),
    'form'    => $form->createView(),
));

我也注意到你有

return $this->render('AleksandarIntelMarketingBundle::contact.html.php');

作为contactAction方法中的第一行。应该删除它,因为你的代码不会超过该行,它只是返回视图。