How to display a month-year dropdown in Symfony2

前端 未结 5 1202
生来不讨喜
生来不讨喜 2020-12-09 11:36

In my application, the user hsa to give a date by only selecting a month and a year in two dropdown lists. How can I achieve that?

Here is what I\'ve tried so far :

5条回答
  •  天命终不由人
    2020-12-09 12:27

    I working on a custom form type which would allow to solve such problem. The solution is based on what @oleg-andreyev proposed. The logic wrapped to a form type.

    Usage:

    add('exp_date', 'payum_credit_card_expiration_date');
    

    The form type code:

    vars['value'])) {
                    $view['day']->vars['value'] = $view['day']->vars['choices'][0]->value;
                }
    
                $style = 'display:none';
                if (false == empty($view['day']->vars['attr']['style'])) {
                    $style = $view['day']->vars['attr']['style'].'; '.$style;
                }
    
                $view['day']->vars['attr']['style'] = $style;
            }
        }
    
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->replaceDefaults(array(
                'years' => range(date('Y'), date('Y') + 9)
            ));
        }
    
        public function getParent()
        {
            return 'date';
        }
    
        public function getName()
        {
            return 'payum_credit_card_expiration_date';
        }
    }
    

    You can copy\paste it or register payum bundle and use this type from (will be available there in next major release).

提交回复
热议问题