2016-11-19 37 views
1

我正在使用FosrestBundle和Symfony3構建API。在我的一些工作中,我必須構建表單,並且我希望表單中的某些字段設置爲「不需要」。但是當我嘗試創建它們時出現錯誤。當使用FosrestBundle構建表單時,在Symfony3中使用「不需要」

下面是我如何爲我的一個實體創建表單:「問題」。

首先我創建了一個QuestionType類,我設定的必要爲false:

class QuestionType extends AbstractType{ 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 

    $builder->add('libelle'); 
    $builder->add('description'); 
    $builder->add('imageRoot'); 
    $builder->add('page', 'integer', array('required' => false)); 
    $builder->add('ligne', 'integer', array('required' => false)); 

} 

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults([ 
     'data_class' => 'AppBundle\Entity\Question', 
     'csrf_protection' => false 
    ]); 
} 
} 

其次,我在validation.yml文件中配置驗證參數:

AppBundle\Entity\Question: 
properties: 
    libelle: 
     - NotBlank: ~ 
     - Type: string 
    description: 
     - NotBlank: ~ 
     - Type: string 
    imageRoot: 
     - Type: string 
    page: 
     - Type: integer 
     - GreaterThan: 
      value: 0 
     - LessThanOrEqual: 
      value: 1000 

    ligne: 
     - Type: integer 
     - GreaterThan: 
      value: 0 
     - LessThanOrEqual: 
      value: 1000 

第三,我創造了我在控制器中的形式

class ContenuController extends Controller{ 

/** 
* @Rest\View(serializerGroups={"contenu"}, statusCode=Response::HTTP_CREATED) 
* @Rest\Post("/lectureContenu/{id}/Questions") 
*/ 
public function postQuestionAction(Request $request) 
{ 
    $em = $this->getDoctrine()->getEntityManager(); 
    $contenu = $em->getRepository('AppBundle:Contenu')->find($request->get('id')); 
    $typeQuestion = $em->getRepository('AppBundle:TypeQuestion')->find(1); 

    if (empty($contenu)) { 
     return new JsonResponse(['message' => 'Contenu de la question inexistant'], Response::HTTP_NOT_FOUND); 
    } 

    $question = new Question(); 
    $question->setContenu($contenu) 
      ->setTypeQuestion($typeQuestion); 

    $form = $this->createForm(QuestionType::class, $question); 
    $form->submit($request->request->all()); // Validation des données 

    if ($form->isValid()) { 
     $em->persist($question); 
     $em->flush(); 

     return $question; 
    } else { 
     return $form; 
    } 

    } 
} 

最後,當我用示例運行我的代碼時以下文件(使用郵差):

{ 
    "libelle": "test", 
    "description": "test", 
    "imageRoot": "test" 
} 

我得到這個錯誤:

... 
"message": "Could not load type \"integer\"", 
"class": "Symfony\\Component\\Form\\Exception\\InvalidArgumentException", 
... 

我不知道爲什麼!對我而言,一切看起來都正確。請幫忙 !

+0

爲什麼在'QuestionType'形式的'buildForm'方法中添加'return $ options;'??? –

+0

這是一個錯誤,我只是刪除它:) – kabrice

+0

請添加AppBundle \ Entity \ Question'實體的驗證約束和相關代碼。 (es:一些自定義約束)。 –

回答

1

我把我的評論作爲一個答案從未答覆的問題中刪除這個問題。

類型的名稱分別爲在Symfony的2.8棄用在Symfony的3

刪除當您添加的孩子表單類型,你必須使用該類型的完全限定域名(FQN)。 你可以(與你的整數型爲例)做2種方式

$builder->add('page', IntegerType::class) 

$builder->add('page', 'Symfony\Component\Form\Extension\Core\Type\IntegerType'); 

如果使用第一個選項,不要忘記導入類定義的類:

use Symfony\Component\Form\Extension\Core\Type\IntegerType; 

所有類型的列表,請訪問:https://symfony.com/doc/current/reference/forms/types.html

相關問題