0

嘿,我是这个 symfony2 框架的新手,所以我需要一些 helo。

这是联系表单的代码,当我尝试将表单呈现到视图页面时,它会出现一些错误,请参见下面的代码和错误。

如果有人知道可能是什么问题,请告诉我..

谢谢!

联系人类型.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>
4

1 回答 1

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。可能应该删除它,因为您的代码不会超过该行并且它只是返回视图。

于 2013-08-07T17:18:16.890 回答