2016-03-31 13 views
2

Tarayıcıların çoğu datetime ve ayrıca datetime-local desteklerini geçerli bir giriş türü olarak bırakıyor. Bu sorunun yazılmasından sonra, datetime-local için datetime'dan (ki neredeyse yok) destek için daha fazla destek var.Symfony formları datetime-local giriş tipini nasıl destekler?

Symfony'nin form oluşturucusunu kullanarak formlar oluşturuyorsanız, datetime'u destekler, ancak datetime-local'u desteklemez. Peki, symfony form oluşturucuyu datetime-local giriş tipini nasıl kabul edersiniz ve giriş tipinin geri kalanını aynı tutmaya devam edersiniz?

cevap

3

Bu sorunun çözülmesinin bir yolu, DateTimeType'un üzerine yazılarak ve bunu kullanarak yapılacak giriş türünün metnini datetime-local olarak değiştirebilirsek.

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

}