2016-06-01 77 views
1

我在symfony的验证文件夹中有user.yml文件,我正在使用它来验证数据。例如:如何在symfony验证(yml)文件中设置所需的false

\APIBundle\Entity\User: 
properties: 
    startDate: 
     - NotBlank: { message: 'Start date cannot be blank.' } 
     - Date: { message: 'Enter the valid start date.' } 

我想验证“startDate”字段,如果用户已通过现在正在工作的字段。但如果用户没有通过“startDate”,那么我想跳过这个验证。即希望这个验证是可选的。

如何做到这一点?

我试图设置需要假如下:

\APIBundle\Entity\User: 
properties: 
    startDate: 
     - Required: false 
     - NotBlank: { message: 'Start date cannot be blank.' } 
     - Date: { message: 'Enter the valid start date.' } 

这是不工作的symfony返回错误

回答

0

只是删除NotBlank

\APIBundle\Entity\User: 
properties: 
    startDate: 
     - Date: { message: 'Enter the valid start date.' } 

NotBlank是相当于reqired

1

所需的只是会影响渲染see

如果为true,HTML5需要的属性将被渲染。相应的标签也会用所需的类进行渲染。

这是肤浅的,独立于验证。充其量,如果你让Symfony猜到你的字段类型,那么这个选项的值将会从你的验证信息中猜出 。

,你应该在你的表单中使用empty_data像这样:

$builder->add('gender', ChoiceType::class, array(
    'choices' => array(
     'm' => 'Male', 
     'f' => 'Female' 
    ), 
    'required' => false, 
    'placeholder' => 'Choose your gender', 
    'empty_data' => null 
)); 

你在这种情况下,实体应当具备的使用性能nullable=true当然像mentionned NotBlank不应该被用来