duanchuo7741 2018-01-30 09:24
浏览 42
已采纳

Symfony TimezoneType表单

In my symfony 3 application, User can choose their timezone in an form.

So i put it in my form like that:

    $builder
    ->add('timezone', TimezoneType::class)

It's working but i want to personnalize what user can choose in the list. Actually i see all country by continent. The list is too large with too many city.

I would like to have a shorter list with only main city and put the UTC before each city. Something like Twitter.

Example:

  • (UTC) London
  • (UTC+01:00) Paris
  • (UTC+02:00) Athens
  • (UTC+03:00) Moscow
  • Etc ...

Does someone can help me on that?

Thanks

  • 写回答

1条回答 默认 最新

  • dtgv52982 2018-01-30 09:38
    关注

    As you can see inside the Timezone Type, Symfony get the timezone this way:

    private static function getFlippedTimezones()
    {
        if (null === self::$timezones) {
            self::$timezones = array();
    
            foreach (\DateTimeZone::listIdentifiers() as $timezone) {
                $parts = explode('/', $timezone);
    
                if (count($parts) > 2) {
                    $region = $parts[0];
                    $name = $parts[1].' - '.$parts[2];
                } elseif (count($parts) > 1) {
                    $region = $parts[0];
                    $name = $parts[1];
                } else {
                    $region = 'Other';
                    $name = $parts[0];
                }
    
                self::$timezones[$region][str_replace('_', ' ', $name)] = $timezone;
            }
        }
    
        return self::$timezones;
    }
    

    You could create your own type or use it inside the form like

     $choices = self::getCustomFlippedTimezones();
     $builder
            ->add('timeZone', ChoiceType::class, array(
                    'choices' => $choices
                ))
    

    For reusability:

    // src/AppBundle/Form/Type/CustomTimeZoneType.php
    namespace AppBundle\Form\Type;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    
    class CustomTimeZoneType extends AbstractType
    {
        private static function getFlippedTimezones() { /*[...]*/}
    
        public function configureOptions(OptionsResolver $resolver)
        {
    
            $resolver->setDefaults(array(
                'choices' => self::getCustomFlippedTimezones(),
                'choices_as_values' => true,
            ));
        }
    
        public function getParent()
        {
            return ChoiceType::class;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入