首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使symfony表单支持datetime-local输入类型?

如何使symfony表单支持datetime-local输入类型?
EN

Stack Overflow用户
提问于 2016-03-31 18:37:57
回答 1查看 645关注 0票数 2

大多数浏览器都放弃了对datetimedatetime-local作为有效输入类型的支持。在撰写本问题时,对datetime-local的支持比对datetime的支持更多(这几乎是不存在的)。

如果使用Symfony的表单构建器构建表单,它支持datetime,但不支持datetime-local。那么如何让symfony form builder接受datetime-local输入类型,并保持输入类型的其余功能不变呢?

EN

回答 1

Stack Overflow用户

发布于 2016-03-31 18:43:43

解决这个问题的一种方法是,如果我们可以将输入类型的文本更改为datetime-local,这可以通过覆盖DateTimeType并使用它来完成。

代码语言:javascript
复制
<?php

namespace AppBundle\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;

class DateTimeType extends \Symfony\Component\Form\Extension\Core\Type\DateTimeType
{
    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['widget'] = $options['widget'];

        // Change the input to a HTML5 datetime input if
        //  * the widget is set to "single_text"
        //  * the format matches the one expected by HTML5
        //  * the html5 is set to true
        if ($options['html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
            $view->vars['type'] = 'datetime-local';
        }
    }

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36330989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档