大多数浏览器都不再支持 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/