Problème avec objet de validation d'un formulaire
Bonjour,
Je rencontre un problème avec un tuto Symfony 2.
Je précise qu'il s'agit d'un formulaire de contact donc, avec envoi d'un mail (appel d'un template pour le mail), sans entité puisque pas nécessaire ici. J'essaie de faire les choses proprement mais pas évident de trouver le bon code. Sur certains tutos sur le net les formulaires de contact sont avec une entité et basta, mais au moins ça semble fonctionner, alors que moi je galère à essayer de faire bien.
J'ai l'erreur suivante quand je poste le formulaire, je comprends bien l'erreur mais n'arrive pas à la résoudre :
Citation:
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
Mon contrôleur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <?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 |
Ma classe de formulaire :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?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 |
Le formHandler :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <?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 |
Et, enfin, la classe "objet" pour la validation :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
<?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 |
Merci d'avance pour vos réponses.