1

这就是我所拥有的:

  • 一个带有月份的选择字段
  • 一个有年份的精选领域

但是在月份选择字段中,我想添加一个默认选项,例如:

<option value="current">Current</option> 

我不知道如何添加默认选项,占位符不是选项。这是我现在的代码,使用 dateType

->add('startDate', DateType::class, [
    'label' => 'Start date',
    'label_attr' => array('class' => 'sr-only'),
    'placeholder' => [
        'year' => 'Year',
        'month' => 'Month',
    ],
    'years' => range(date('Y')-70, date('Y')),
])

谁能帮我解决这个问题?谢谢!

4

2 回答 2

3

设置默认值使用, array('data' => new \DateTime())

->add('startDate', DateType::class, [
    'label' => 'Start date',
    'label_attr' => array('class' => 'sr-only'),
    'placeholder' => [
       'year' => 'Year',
       'month' => 'Month',
       ],
    'years' => range(date('Y')-70, date('Y')),
    'data' => new \DateTime(),
])
于 2017-05-20T10:00:24.413 回答
0

range用于生成数组,因此最简单的方法就是在数组的开头添加您的值。

就像是:

'years' => array_merge(['current'=>'current'], range(date('Y')-70, date('Y')))

于 2017-04-17T03:52:09.697 回答