Search code examples
symfony2-easyadmin

EasyAdmin Symfony Date start from 2012 and end 2022


I'm new with Symfony and EasyAdmin. In my entity I have a birthday. But when I show it, it shows the start year 2012, end 2022. How can I fix it?

This is the code:

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

Solution

  • You're experiencing a Symfony default year range. To solve this, you can construct your own DateTime type like this:

    <?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));
        }
    }
    

    You could then instruct EasyAdmin to use this type like so:

    - { property: 'occurance', 
        type: 'AppBundle\AdminForm\Field\MyCustomDateType', 
        label: 'Date and time' }