我有一個很大的bug,只花了2個小時時間... 我改變了我的FlightType中的configureOptions方法,但它不是解決方案。可捕獲致命錯誤:傳遞給(...) Handler FlightHandler :: __ construct()的參數1必須是Symfony Component Form Form的一個實例
錯誤:
Catchable Fatal Error: Argument 1 passed to Test\FrontBundle\Form\Handler\FlightHandler::__construct() must be an instance of Symfony\Component\Form\Form, none given, called in /Users/macbookpro/Desktop/Projets Web/Emirates/app/cache/dev/appDevDebugProjectContainer.php on line 834 and defined
我剛編碼我service.yml這樣的:
services:
flight_form:
factory_service: form.factory
factory_method: createNamed
class: Symfony\Component\Form\Form
arguments:
- flight
- flight_form
flight_type:
class: Test\FrontBundle\Form\Type\FlightType
tags:
- { name: form.type, alias: flight_form }
flight_handler:
class: Test\FrontBundle\Form\Handler\FlightHandler
argument: [@flight_form, @request]
scope: request
FlightController.php
public function createAction(Request $request) {
$formHandler = $this->get("flight_handler");
$form = $formHandler->getForm();
if ($formHandler->process()) {
$em = $this->getDoctrine()->getManager();
$em->persist($form->getData());
$em->flush();
return $this->redirect($this->generateUrl("test_front_flight_list"));
}
return $this->render('TestFrontBundle:Flight:create.html.twig', array('form' => $form->createView()));
}
}
FlightHandler.php
(...)
class FlightHandler {
protected $form;
protected $request;
public function __construct(Form $form, Request $request) {
$this->form = $form;
$this->request = $request;
}
public function getForm() {
return $this->form;
}
public function process() {
$this->form->handleRequest($this->request);
if ($this->request->isMethod("POST") && $this->form->isValid()) {
return true;
}
return false;
}
}
FlightType.php
public function setDefaultOptions(OptionsResolverInterface $resolver) {
/** @var OptionResolver $resolver */
$this->configureOptions($resolver);
}
public function configureOptions(OptionsResolver $resolver){
$resolver->setDefaults(array(
"data_class" => 'Test\FrontBundle\Entity\Flight'
));
我看到你有三種不同的方式使用'flight_form':首先你定義一個名爲'flight_form'的服務。然後你說這個服務的一個參數是'flight_form'。最後你說'flight_form'是'flight_type'服務的別名。這非常混亂。我建議你明確這一點 –