我是Symfony和EasyAdmin的新手。在我的实体中,我有一个生日。但是当我展示它时,它显示了 2012 年开始,2022 年底。我该如何解决它?
这是代码:
/**
* @var date
*
* @ORMColumn(name="birth_day", type="date")
*/
private $birthDay;
您正在经历Symfony默认的年份范围。要解决此问题,您可以像这样构造自己的 DateTime 类型:
<?php
namespace AppBundleAdminFormField;
use SymfonyComponentFormExtensionCoreTypeDateTimeType;
use SymfonyComponentOptionsResolverOptionsResolver;
class MyCustomDateType extends DateTimeType
{
public function configureOptions(OptionsResolver $resolver)
{
// Set the defaults from the DateTimeType we're extending from
parent::configureOptions($resolver);
// Override: Go back 20 years and add 20 years
$resolver->setDefault('years', range(date('Y') - 20, date('Y') + 20));
// Override: Use an hour range between 08:00AM and 11:00PM
$resolver->setDefault('hours', range(8, 23));
}
}
然后,您可以指示 EasyAdmin 像这样使用此类型:
- { property: 'occurance',
type: 'AppBundleAdminFormFieldMyCustomDateType',
label: 'Date and time' }
嗨,我的这个问题的解决方案。 测试类型.php在表单文件夹下是我的表单构建器类。
use SymfonyComponentFormExtensionCoreTypeDateType;
类 TestType 扩展 AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('name')
->add('birtdate',DateType::class, array(
'widget' => 'choice',
// this is actually the default format for single_text
'format' => 'yyyy-MM-dd',
'years' => range(date('Y')-80, date('Y'))
));
}
参考链接 https://symfony.com/doc/current/reference/forms/types/date.html#data-class