2016-03-31 31 views
2

大多數瀏覽器都將支持datetime以及datetime-local作爲有效的輸入類型。截至撰寫本問題時,支持datetime-local的支持多於​​(幾乎不存在)。如何使symfony表單支持datetime-local輸入類型?

如果您使用Symfony的表單構建器構建表單,它支持datetime但不支持datetime-local。那麼如何讓symfony表單生成器接受datetime-local輸入類型並保持輸入類型的其餘功能相同?

回答

3

這個問題可以解決的一個方法是,如果我們可以改變輸入類型的文本來說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'; 
     } 
    } 

} 
相關問題