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 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示