symfony2日期字段类型格式选项将输入类型更改为文本

时间:2013-07-25 11:54:04

标签: date symfony types format

当我这样做时:

    $builder->add('submittedAt_min',
                    'date',
                    array('widget' => 'single_text', 'label' => 'my label', 'attr' => array('placeholder' => 'my placeholder')));

我得到:

    <input type="date" placeholder="my placeholder" required="required" name="es_app_filter[submittedAt_min]" id="es_app_filter_submittedAt_min" class="hasDatepicker">

类型为&#34; date&#34;

但是当我将格式选项添加到字段时:

    $builder->add('submittedAt_min',
                    'date',
                    array('format' => 'dd-MM-yyyy', 'widget' => 'single_text', 'label' => 'my label', 'attr' => array('placeholder' => 'my placeholder')));

我明白了:

      <input type="text" placeholder="my placeholder" required="required" name="es_app_filter[submittedAt_min]" id="es_app_filter_submittedAt_min">

输入类型现在是&#39; text&#39;。

我基于[type =&#34; date&#34;]在jQuery中做了几件事。当它变为&#34; text&#34;我所有的行动都不会奏效。

任何想法为什么会这样,如何修复它,任何解决方法?

2 个答案:

答案 0 :(得分:3)

所以我刚收到Symfony IssueTracker的答案。

See comment here https://github.com/symfony/Form/blob/master/Extension/Core/Type/DateType.php#L130
The field type is set to "date" only if the format matches the one expected by HTML5 ( yyyy-MM-dd ) 

评论是:

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

https://github.com/symfony/Form/blob/master/Extension/Core/Type/DateType.php#L130

这不是一个错误,它是一个功能。

答案 1 :(得分:0)

您是否尝试将数组后的格式放入数组中(如symfony2文档中的示例)? http://symfony.com/doc/current/reference/forms/types/date.html#format

相关问题