我对使用Symfony 2.4.1的表单验证对象有疑问。它是一个没有实体的联系表单,但是有一个处理程序和一个用于邮件内容的Twig模板。
错误是:
ContextErrorException: Catchable Fatal Error: Argument 1 passed to Test\MyBundle\Form\Handler\ContactHandler::onSuccess() must be an instance of Test\MyBundle\Form\Model\Contact, array given, called in C:\Program Files\wamp\www\sf2\src\Test\MyBundle\Form\Handler\ContactHandler.php on line 28 and defined in C:\Program Files\wamp\www\sf2\src\Test\MyBundle\Form\Handler\ContactHandler.php line 36
控制器:
<?php
// src/Test/MyBundle/Controller/MyController.php
namespace Test\MyBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Test\MyBundle\Form\Type\ContactType;
use Test\MyBundle\Form\Handler\ContactHandler;
use Symfony\Component\HttpFoundation\Session\Session;
class MyController extends Controller {
public function contactAction(Request $request) {
$form = $this->get('form.factory')->create(new ContactType());
$request = $this->getRequest();
$formHandler = new ContactHandler($form, $request, $this->get('mailer'),$this->get('templating'));
$process = $formHandler->process();
if ($process) {
$this->get('session')->getFlashBag('success', 'Merci de nous avoir contacté, nous vous répondrons dans les meilleurs délais.');
$this->redirect($this->generateUrl('test_my_contact_success'));
}
return $this->render("TestMyBundle:My:contact.html.twig",
array("form" => $form->createView(),
"hasError" => $request->getMethod() == 'POST' && !$form->isValid()
)
); // -- render
} // -- contactAction()
} // -- classe
表单类型类:
<?php
namespace Test\MyBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ContactType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('nom', 'text', array('max_length' => 30, 'label' => 'Prénom, nom*', 'required' => false, 'trim' => true, 'attr' => array('size'=>'30')))
->add('courriel', 'email', array('max_length' => 255, 'label' => 'Adresse email*', 'required' => false, 'trim' => true, 'attr' => array('size'=>'30')))
->add('sujet', 'text', array('max_length' => 255, 'label' => 'Objet*', 'trim' => true, 'attr' => array('size'=>'30')))
->add('msg', 'textarea', array('label' => 'Message*', 'attr' => array('rows' => '10','cols' => '80')))
->add('submit', 'submit');
} // -- buildForm()
public function setDefaultOptions(OptionsResolverInterface $resolver) {
return array('data_class' => 'Test\MyBundle\Form\Model\Contact');
} // -- setDefaultOptions()
public function getName() {
return 'Contact';
} // -- getName()
} // -- classe
处理程序类:
<?php
// src/Test/MyBundle/Form/Handler/Handler.php
namespace Test\MyBundle\Form\Handler;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\EngineInterface;
use Test\MyBundle\Form\Model\Contact;
class ContactHandler {
protected $request;
protected $form;
protected $mailer;
protected $templating;
public function __construct(Form $form, Request $request, $mailer, EngineInterface $templating) {
$this->form = $form;
$this->request = $request;
$this->mailer = $mailer;
$this->templating = $templating;
} // -- __construct()
public function process() {
if ('POST'==$this->request->getMethod()) {
$this->form->handleRequest($this->request);
if ($this->form->isValid()) {
$contact = $this->form->getData();
$this->onSuccess($contact);
return true;
} // -- isValid()
} // -- si POST
return false;
} // -- process()
protected function onSuccess(Contact $contact) {
$oMessage = \Swift_Message::newInstance()
->setContentType('text/html')
->setSubject($contact->getSujet())
->setFrom($contact->getCourriel())
->setTo('me@toto.com') ->setBody($this->templating->render('@TestMyBundle:My:Mails/contact.html.twig',
array('nom' => $contact->getNom(),
'msg' => $contact->Msg()
)
)
);
return $this->mailer->send($oMessage);
} // -- onSuccess()
} // -- classe
对象验证类:
<?php
// src/Test/MyBundle/Form/Model/Contact.php
namespace Test\MyBundle\Form\Model;
class Contact {
protected $nom;
protected $courriel;
protected $sujet;
protected $msg;
public function getNom() {
return $this->nom;
}
public function setNom($nom) {
$this->nom = $nom;
}
public function getCourriel() {
return $this->courriel;
}
public function setCourriel($courriel) {
$this->courriel = $courriel;
}
public function getSujet() {
return $this->telephone
}
public function setSujet($sujet) {
$this->sujet = $sujet;
}
public function getMsg() {
return $this->msg;
}
public function setMsg($msg) {
$this->msg = $msg;
}
} // -- classe
感谢。
答案 0 :(得分:0)
为什么不直接在控制器内发送电子邮件?
问题在于:
if ($this->form->isValid()) {
$contact = $this->form->getData(); // returns an array
$this->onSuccess($contact); // onSuccess needs $contact to be an object
return true;
}
尝试将数组转换为object:
if ($this->form->isValid()) {
$contact = (object)$this->form->getData();
$this->onSuccess($contact);
return true;
}
如果强制转换不起作用,请尝试更改onSuccess()函数内的Contact对象中的参数,如:
$contact['subjet']