我想在一个月内查询一些数据,所以我写了一个表格,添加了一些字段,但我发现我无法在Symfony2的date
中选择一个月,我在SF搜索过它并找到了这样:
show month number instead of month name on symfony2 formbuilder
我很开心~~ 不幸的是,我发现无法正常工作,错误信息是:
The "format" option should contain the letters "y", "M" and "d".
可能原因是Symfony2的版本,我使用的是版本2.5.x.
代码:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$school_id = $this->user->getSchoolId();
$classes = $this->container->get('doctrine')->getRepository('ProjCoreBundle:TbBizClass')->findBySchoolId($school_id);
$classes_arr = array(0 => 'All Classes');
foreach ($classes as $class) {
$classes_arr[$class->getClassId()] = $class->getClassName();
}
$builder->setMethod('GET');
$builder->add('classId', 'choice', array(
'choices' => $classes_arr,
));
$builder->add('status', 'choice', array(
'choices' => array(
0 => 'All',
Checkroll::STATUS_ATTD_CASUAL => 'Casual',
Checkroll::STATUS_ATTD_SICK => 'Sick',
),
));
$builder->add('createdAt', 'date', array(
'format' => 'MMM-yyyy',
'years' => range(date('Y'), date('Y')+12),
'days' => array(1),
'empty_value' => array('year' => '----', 'month' => '----', 'day' => false)
));
}
答案 0 :(得分:0)
我发现了这个:
Symfony 2: How to render date field without a day?
非常好的答案!
接下来是从他的回答中复制的:
您可以在twig文件中执行以下操作。
{{ form_widget(yourForm.month.month) }}
{{ form_widget(yourForm.month.year) }}