2016-02-28 25 views
2

餘米訓練,但我下的symfony是3如何使用自定義FormType在Symfony的3

我有問題,我得到這個錯誤

預期類型「串」, 「測試的參數\ FrontBundle \表格\型號\ SheetType」給出

上SheetType.php代碼

<?php 

namespace Test\FrontBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\Extension\Core\Type\DateType; 
use Symfony\Component\Form\FormBuilderInterface; 


class SheetType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('name',null,array('label'=>'Titre de l\'album')) 
      ->add('type') 
      ->add('artist') 
      ->add('duration') 
      ->add('released', DateType::class) 
      ; 
    } 
} 

和我SheetController.php我這樣做,構成了我控制器 我不知道我可以解決這一切時我嘗試別的我得到錯誤

public function createAction(Request $request) 
    { 
     $form = $this->createForm(new SheetType()); 

     $form->handleRequest($request); 

     if($request->isMethod('post') && $form->isValid()){ 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($form->getData()); 
      $em->flush(); 
      return $this->redirect($this->generateUrl('test_front_sheet_list')); 
     } 
     return $this->render('TestFrontBundle:Sheet:create.html.twig', array('form' => $form->createView())); 
    } 

回答

5

由於symfony的2.8你必須通過一個完整的限定類名稱實例作爲創建表單或表單生成器時的參數,它不再需要一個FormTypeInterface的實例。

看到https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.8.md

所以,你應該用$form = $this->createForm(SheetType::class);代替。

+0

我們現在該如何在formType中傳遞自定義屬性?我曾經這樣做過:'new MyFormType($ customAttr)'但現在因爲我們必須通過表單類型的全限定類名,所以我不能再這樣做了。 – Delphine

+0

使用了一個選項;) – Heah

相關問題