Symfony:如何从子窗体中启用验证错误以显示在父窗体中?

时间:2016-10-27 12:06:08

标签: validation symfony

我有Namespace\Entity\MainEntity的表单,其中包含如下子表单:

->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字段的验证?

1 个答案:

答案 0 :(得分:2)

启用(master $=) $ git merge dev --strategy=theirs documentation),让您的子表单错误显示在父表单中。

PrintWriter output; output=createWriter(*someDirectory*); //such as data/outputText.txt String [] previousText = loadStrings (*someDirectory*) //same as above String newText= "This is a new line that won't destroy entire text file"; for (int i=0; i<previousText.length; i++) { output.println(previousText[i]); } output.println(newText); output.flush(); output.close(); 使用以下验证映射:

error_bubbling

Valid约束添加到SubFormEntity的验证映射中:

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

MainEntity选项添加到 Namespace\Entity\MainEntity: properties: property2: - Valid

error_bubbling

...或在您的SubFormType中加入public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( // ... 'error_bubbling' => true )); } 时动态添加选项:

SubFormType