【问题标题】:EasyAdmin Symfony Date start from 2012 and end 2022EasyAdmin Symfony 日期从 2012 年开始到 2022 年结束
【发布时间】:2017-07-26 04:44:37
【问题描述】:

我是 Symfony 和 EasyAdmin 的新手。在我的实体中,我有一个生日。但是当我显示它时,它会显示 2012 年开始,2022 年结束。我该如何解决?

这是代码:

/**
 * @var \date
 *
 * @ORM\Column(name="birth_day", type="date")
 */
private $birthDay;

【问题讨论】:

    标签: symfony2-easyadmin


    【解决方案1】:

    您正在体验 Symfony 默认的年份范围。为了解决这个问题,您可以像这样构造自己的 DateTime 类型:

    <?php
    
    namespace AppBundle\AdminForm\Field;
    
    use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    
    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: 'AppBundle\AdminForm\Field\MyCustomDateType', 
        label: 'Date and time' }
    

    【讨论】:

      【解决方案2】:

      您好,我解决了这个问题。 表单文件夹下的 TestType.php 是我的表单构建器类。

      use Symfony\Component\Form\Extension\Core\Type\DateType;
      

      类 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

      【讨论】:

        猜你喜欢
        • 2021-04-12
        • 1970-01-01
        • 2021-07-25
        • 2022-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-12
        相关资源
        最近更新 更多