2016-10-27 8 views
1

私はこのようなサブフォームが含まNamespace\Entity\MainEntityするためのフォームがあります。以下に示すようにSymfony:サブフォームの検証エラーを親フォームに表示するにはどうすればいいですか?

->add('property1', TextType::class, array(
    'required' => false, 
    'label'  => 'This is the form label', 
    'data_class' => 'Namespace\Entity\SubFormEntity', 
    'attr'  => array(
     'class' => 'form-control' 
    ) 
)) 
->add('property2', SubFormType::class) 

SubFormType自体がテキストフィールドを持っています

->add('subproperty1', TextType::class, array(
    'label' => false, 
    'attr' => array(
     'class' => 'form-control' 
    ) 
)) 

私はフォームを送信した場合、property1が正しく検証されますproperty2の検証は実行されず、subform_fieldの値が正しくない場合でもフォームが送信されます。

私が試してみました

...

Namespace\Entity\SubFormEntity: 
    properties: 
     property2: 
      - Type: 
       type: numeric 

...と...

Namespace\Entity\MainEntity: 
    properties: 
     property2.subproperty1: 
      - Type: 
       type: numeric 

にはどうすればsubproperty1フィールドの検証を有効にすることができますか?

答えて

2

documentation)にサブフォームのエラーを親フォームに表示させる。

使用SubFormEntityための次の検証マッピング:

Namespace\Entity\MainEntity: 
    properties: 
     property2: 
      - Valid 

があなたのSubFormTypeerror_bubblingオプションを追加します:

Namespace\Entity\SubFormEntity: 
    properties: 
     subproperty1: 
      - Type: 
       type: numeric 

があなたのMainEntityの検証マッピングにValid制約を追加

public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     // ... 
     'error_bubbling' => true 
    )); 
} 

...またはあなたのSubFormTypeを含むとき、このようなあなたのParentFormTypeで動的にオプションを追加します。

->add('property2', SubFormType::class, array(
    'error_bubbling' => true 
)) 
+0

は、あなたの答えをありがとう! – stax

関連する問題