5

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;
Pang
  • 9,564
  • 146
  • 81
  • 122
inez pro
  • 53
  • 1
  • 4

2 Answers2

5

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' }
1sloc
  • 1,180
  • 6
  • 12
1

Hi my solution for this problem. TestType.php under the Form Folder is my form builder class.

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

class TestType extends 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'))    
    ));
}

reference link is https://symfony.com/doc/current/reference/forms/types/date.html#data-class

ureyni
  • 71
  • 6