symfony - symfony2 contact form error on rendering to the view page -
hey i'm new in symfony2 framework need helo.
this code contact form , when i'm trying render form view page gets error see code bellow , error also.
if 1 knows might problem please let me know..
thanks!
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 name?', 'pattern' => '.{2,}' //minlength ) )) ->add('email', 'email', array( 'attr' => array( 'placeholder' => 'so can you.' ) )) ->add('subject', 'text', array( 'attr' => array( 'placeholder' => 'the subject of message.', 'pattern' => '.{3,}' //minlength ) )) ->add('message', 'textarea', array( 'attr' => array( 'cols' => 20, 'rows' => 2, 'placeholder' => 'and message me...' ) )); } public function setdefaultoptions(optionsresolverinterface $resolver) { $collectionconstraint = new collection(array( 'name' => array( new notblank(array('message' => 'name should not blank.')), new length(array('min' => 2)) ), 'email' => array( new notblank(array('message' => 'email should not blank.')), new email(array('message' => 'invalid email address.')) ), 'subject' => array( new notblank(array('message' => 'subject should not blank.')), new length(array('min' => 3)) ), 'message' => array( new notblank(array('message' => 'message should not blank.')), new length(array('min' => 5)) ) )); $resolver->setdefaults(array( 'constraints' => $collectionconstraint )); } public function getname() { return 'contact'; } } ?>
the controller
<?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() ); } }
the routing
aleksandar_intel_marketing_contactpage: pattern: /contact defaults: { _controller: aleksandarintelmarketingbundle:default:contact }
and contact.html.php on view folder
<?php echo $view['form']->form($form) ?>
now when add the code above in contact.html.php got fallowing
notice: undefined variable: form in c:\wamp\www\symfony\src\aleksandar\intelmarketingbundle\resources\views\contact.html.php line 1 500 internal server error - contexterrorexception
the code of view
<!-- 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>
you need add form
block:
$this->renderview('aleksandarintelmarketingbundle::contact.html.php', array( 'ip' => $request->getclientip(), 'name' => $form->get('name')->getdata(), 'message' => $form->get('message')->getdata(), 'form' => $form->createview(), ));
i noticed have
return $this->render('aleksandarintelmarketingbundle::contact.html.php');
as first line inside contactaction
method. should remove since code not make past line , it's returning view.
Comments
Post a Comment