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

标签 php html datetime symfony formbuilder

大多数浏览器都不再支持 datetime 以及 datetime-local 作为有效输入类型。截至撰写此问题时,已有更多支持datetime-local的支持比datetime (这几乎不存在)。

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

最佳答案

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

<?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';
        }
    }

}

关于php - 如何使 symfony 表单支持 datetime-local 输入类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36330989/

相关文章:

javascript - 如何使用Javascript将<pre>中的CSV数据转换为HTML表格?

javascript - Angularjs 使用 $touched 输入重置表单

datetime - 如何在 rails 3.0.1 中更改时间格式

python - 如何在Python中将日期时间转换为字符串?

php - 时区和播出日期

javascript - 在大型网站中使用 .before 弹出新的 div,隐藏在索引页上

php - 在嵌套循环中使用数组耗尽内存

.net - 如何从 PHP 使用 WCF Web 服务?

PHP 获取 SWF 文件的尺寸

html - 如何将 figcaption 放在图像顶部