范围为日期时间的Symfony问题(窗体)

问题描述 投票:-1回答:1

我不明白我的问题。如果您能给我一个想法。我有一个dateTime字段可以工作,但是我的领域年份范围很广(2015-2025)。我想要更大的范围(例如:1960-2040)。

class BookType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('author')
            ->add('publicationDate')
            ->add('format')
            ->add('language')
            ->add('user')
            ->add('excelFile', FileType::class,[
                'label' => 'Fichier excel (xls/xlsx file)',
                'mapped' => false,
                'required' => false,
                'constraints' => [
                    new File([
                        'maxSize' => '1024k',
                        'mimeTypes' => [
                            'application/vnd.ms-excel',
                            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        ],
                        'mimeTypesMessage' => 'Please upload a valid xls/xlsx document',
                    ])
                ]
            ])
        ;
    }

php symfony symfony4
1个回答
0
投票

您可以在这里看到DateType::Class Refrence。该字段将具有配置您的选择框行为的选项。您可以简单地使用php的range()函数在代码中映射年份,例如:range(1960,2040)

$builder->add('field', DateType::Class , [
    'years' => range(date('Y')-50, date('Y')+50) // Lists 50 Years Before and After
]);

在您的代码中只需使用:

class BookType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title')
            ->add('author')
            ->add('publicationDate', DateType::Class , [
                'years' => range(1960, date('Y')+20) // Change As eper your requirements
            ])
            ->add('format')
            ->add('language')
            ->add('user')
            ->add('excelFile', FileType::class,[
                'label' => 'Fichier excel (xls/xlsx file)',
                'mapped' => false,
                'required' => false,
                'constraints' => [
                    new File([
                        'maxSize' => '1024k',
                        'mimeTypes' => [
                            'application/vnd.ms-excel',
                            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                        ],
                        'mimeTypesMessage' => 'Please upload a valid xls/xlsx document',
                    ])
                ]
            ])
        ;
    }
© www.soinside.com 2019 - 2024. All rights reserved.