Repository: symfony/symfony
File: src/Symfony/Component/Form/Extension/Core/Type/BirthdayType.php (PHP)

Code before:

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BirthdayType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $options = $resolver->resolve();
        $resolver->setDefaults([
            'invalid_message' => 'Please enter a valid birthdate.',
        ]);
        if ($options['widget'] === 'single_text') {
            $resolver->setDefaults([
                'attr' => ['min' => date('Y-m-d', strtotime('120 years ago')), 'max' => date('Y-m-d')],
            ]);
        } else {
            $resolver->setDefaults([
                'years' => range((int) date('Y') - 120, date('Y')),
            ]);
        }

        $resolver->setAllowedTypes('years', 'array');
    }

    public function getParent(): ?string
    {
        return DateType::class;
    }

    public function getBlockPrefix(): string
    {
        return 'birthday';
    }
}


Review comment (bug):
This is the wrong way to do it:

- default values for `attr` is lost as soon as *any* value is passed for `attr`. It is not merged with the default
- you are no longer defining the `years` option (while defining that its only allowed type is an array)
- resolving options inside `configureOptions` is totally broken, and will not take into account the options provided by the user when using that form type to create a form (as they are not available in your call to `resolve()`. We have a way to set smart defaults in the resolver but that's not done by resolving options incorrectly.

To me, you should be using the `buildView` option to add some default attributes in `$view->vars['attr']` based on the configured years instead.

Code after:

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BirthdayType extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'years' => range(date('Y') - 120, date('Y')),
            'invalid_message' => 'Please enter a valid birthdate.',
        ]);

        $resolver->setAllowedTypes('years', 'array');
    }

    public function getParent(): ?string
    {
        return DateType::class;
    }

    public function getBlockPrefix(): string
    {
        return 'birthday';
    }

    public function buildView(FormView $view, FormInterface $form, array $options): void
    {
        if ('single_text' === $options['widget']) {
            $view->vars['attr']['min'] ??= \sprintf('%d-01-01', min($options['years']));
            $view->vars['attr']['max'] ??= \sprintf('%d-12-31', max($options['years']));
        }
    }
}