2016-07-04 79 views
2

所以我試圖通過FormHandler通過它們各自的服務來使用FormType來獲得相當乾淨的Form流。 現在的問題是,我在Symfony 3.0中出現錯誤: Expected argument of type "string", "AppBundle\Form\Type\CurrencyType" givenFormType和FormHandler作爲Symfony3中的服務

我不太確定如何在新版本中使用它。

的FormType:

/** 
* Class CurrencyType 
* @package AppBundle\Form\Type 
*/ 
class CurrencyType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('displayName', TextType::class) 
      ->add('symbol', TextType::class) 
      ->add('save', SubmitType::class, array('label' => 'Voeg Currency toe')) 
     ; 
    } 

    public function setDefaultOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Currency' 
     )); 
    } 
} 

表單處理程序:

class CurrencyFormHandler 
{ 
    /** 
    * @var Request 
    */ 
    protected $requestStack; 

    /** 
    * @var CurrencyManager 
    */ 
    protected $currencyManager; 

    /** 
    * @var FormInterface 
    */ 
    protected $form; 

    /** 
    * CurrencyFormHandler constructor. 
    * @param FormInterface $form 
    * @param RequestStack $requestStack 
    * @param CurrencyManager $currencyManager 
    */ 
    public function __construct(FormInterface $form, RequestStack $requestStack, CurrencyManager $currencyManager) 
    { 
     $this->form = $form; 
     $this->requestStack = $requestStack; 
     $this->currencyManager = $currencyManager; 
    } 

    /** 
    * Process the form 
    * 
    * @param Currency|null $currency 
    * @return bool 
    */ 
    public function process(Currency $currency = null) 
    { 
     if (null === $currency) { 
      $holiday = $this->currencyManager->create(); 
     } 
     $this->form->setData($currency); 
     $this->form->handleRequest($this->requestStack->getCurrentRequest()); 
     if ($this->form->isValid()) { 
      $this->onSuccess($currency); 
      return true; 
     } 
     return false; 
    } 

    /** 
    * When the form is succesfully posted 
    * 
    * @param Currency $currency 
    */ 
    public function onSuccess(Currency $currency) 
    { 
     $this->currencyManager->save($currency); 
    } 
} 

的服務:

<service id="app.currency.form" class="Symfony\Component\Form\Form"> 
      <argument>app_currency</argument> 
      <argument type="service" id="app.currency.form.type" /> 
      <factory method="createNamed" service="form.factory" /> 
     </service> 
     <service id="app.currency.form.type" class="AppBundle\Form\Type\CurrencyType"> 
      <tag name="form.type" alias="currencyform"/> 
     </service> 
     <service id="app.currency.form.handler" 
       class="AppBundle\Form\Handler\CurrencyFormHandler"> 
      <argument type="service" id="app.currency.form" /> 
      <argument type="service" id="request_stack" /> 
      <argument type="service" id="app.currency.manager"/> 
     </service> 

而且控制器:

/** 
* Class AddController 
* @package AppBundle\Controller\Currency 
*/ 
class AddController extends Controller 
{ 
    /** 
    * @Route("/currency/add", name="currency_add") 
    * @param Request $request 
    * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response 
    */ 
    public function indexAction(Request $request) 
    { 
     $form = $this->get('app.currency.form'); 
     $formHandler = $this->get('app.currency.form.handler'); 
     $process = $formHandler->process(); 

     if ($process) { 
      $this->get('session')->getFlashBag()->add('success', 'Currency succesvol toegevoegd'); 
      return $this->redirect($this->generateUrl('currency_overview')); 
     } 

     return $this->render(':currency:add.html.twig', [ 
      'form' => $form->createView(), 
     ]); 
    } 
} 

那麼究竟是什麼錯誤呢?這用於在Symfony 2中始終工作

+1

這個正在傳遞您的表單類型的一個實例。 S3只能使用一個類名,因此會出錯。將您的服務定義更改爲注入字符串而不是服務。這有點奇怪,但表單工廠將使用基於完全限定類名的表單類型服務。這是您會遇到的幾種主要形式的休息時間之一。最好重新閱讀表格上的手冊,幾乎從S3開始。 – Cerad

回答

0

您的表單處理程序看起來不正確。 您聲明:

/** 
* @var CurrencyManager 
*/ 
protected $currencyManager; 

但是,我相信默認情況下是一個字符串。 然後,你這樣做:

public function onSuccess(Currency $currency) 
{ 
    $this->currencyManager->save($currency); 
} 

試圖將其設置爲「貨幣」,而不是「字符串」!