在Symfony中的DateType字段中删除约束验证

时间:2016-06-18 12:05:58

标签: php forms validation symfony

我有一个带有日期字段的实体,我的问题是我想让用户离开字段的可能性月份和年份为空。 所以我想在我的日期字段中完全删除约束验证

我该怎么做?

以下是我的字段

    ->add('datefield', DateType::class, array(
        'data' => new \DateTime(),
        'format' => 'dd MMMM yyyy',
        'required' => false,
        'placeholder' => array(
            'month' => 'empty',
            'year' => 'empty'
        )
    ))

和我的实体

/**
 * @var \DateTime
 *
 * @ORM\Column(name="datefield", type="datetime", nullable=true)
 */
protected $datefield;

这是我的错误

This value is not valid.    datefield    Symfony\Component\Validator\ConstraintViolation

Object(Symfony\Component\Form\Form).children[datefield] = [year => null, month => null, day => 18]

Caused by: Symfony\Component\Form\Exception\TransformationFailedException

Unable to reverse value for property path "datefield": The fields "year", "month" should not be empty

Caused by: Symfony\Component\Form\Exception\TransformationFailedException

The fields "year", "month" should not be empty

1 个答案:

答案 0 :(得分:0)

你是否也尝试过这样的空值:

<resources>>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
        <item name="preferenceStyle">@style/PreferenceThemeOverlay.v14.Material</item>
    </style>

    <style name="MyActionBarTheme" parent="android:Widget.Material.Light.ActionBar">
        <item name="android:background">@color/colorAccent</item>
        <item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
    </style>

    <style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Material.Widget.ActionBar.Title">
        <item name="android:textColor">@color/colorPrimary</item>
    </style>

    <style name="AppTheme" parent="android:Theme.Material.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:colorBackground">@color/background_material_light</item>
        <item name="android:colorPrimary">@color/primary_material_light</item>
        <item name="android:actionBarStyle">@style/MyActionBarTheme</item>
        <item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
        <item name="preferenceStyle">@style/PreferenceThemeOverlay.v14.Material</item>
    </style>
</resources>

我不确定这是否能解决问题。试试吧。