Search code examples
symfonysymfony-forms

Disable days and month from Symfony DateType::class


->add('attendFrom', DateType::class, array(
                'widget' => 'choice',
                'html5' => false,
                'months' => array(),
                'days' => array(),
                'attr' => array(
                    'placeholder' => 'Start year, e.g., 1980 ',
                )
            ))

There is type from which I try to disable days and month. I want to show only years. Is this is possible?

I had find some solution to hide days and months from a twig but I wonder if I can disable this from FormType.

Cheers


Solution

  • Put yyyy in format option

    Form:

    ->add('attendFrom', DateType::class, array(
        'format' => 'yyyy',
        'widget' => 'choice',
        'html5' => false,
        'attr' => array(
            'placeholder' => 'Start year, e.g., 1980 ',
        )
    ))
    

    Update-1

    Hide month and day control in twig

    TWIG:

    {{ 
        form_widget(
            form.formName.attendFrom.day, 
            { 
                'attr': { 'style': 'display:none' }
            }
        )
        form_widget(
            form.formName.attendFrom.month, 
            { 
                'attr': { 'style': 'display:none' }
            }
        ) 
    }}
    

    Ref: Link