Symfony 2.8 Forms / Twig:Twig正在渲染我的输入(type = date)

我期望在呈现页面时,它将被输入(type = date).但我得到的是type = text.

<html>
    <head></head>
    <body>
        <input id="test_form_studentdob" class="normal" type="text" placeholder="DD-MM-YYYY" name="test_form[studentdob]">
    </body>
</html>

我已经尝试过了:

>多个浏览器(桌面和移动)
>删除了我正在加载的所有CSS / JS.这包括加载jQuery / jQuery / bootstrap UI库

我有什么想法可能做错了吗?谢谢

Symfony控制器的片段:

/**
 * @Route("/test")
 */
public function testAction(Request $request) {
    $form = $this->createForm(new TestFormType());
    $form->handleRequest($request);

    return $this->render(
        "AppBundle:SelfService:TestForm.html.twig"
        , array("form" => $form->createView())
    );
}

Symfony Form类型:

<?php
// src/appBundle/Form/TestFormType.php
namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType
    , Symfony\Component\Form\FormBuilderInterface
    , Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TestFormType extends AbstractType {
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(
            array(
                "attr" => array(
                        "id" => "testform"
                )
            )
        );
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
            ->setMethod("GET")
            ->add(
                "studentdob"
                , "date"
                , array(
                    "attr" => array(
                        "class" => "normal"
                        , "placeholder" => "DD-MM-YYYY"
                    )
                    , "format" => "dd-MM-yyyy"
                    , "required" => false
                    , "widget" => "single_text"
                )
            );
    }
}

这是我的Twig模板:

{% block body %}
    {{ form_widget(form.studentdob) }}
{% endblock %}

最佳答案
HTML date input具有独特的格式:yyyy-mm-dd

A valid full-date as defined in 07001, with the additional
qualification that the year component is four or more digits
representing a number greater than 0.

如果你想使用另一种格式,你必须使用文本输入.

因此,当您指定其他格式时,Symfony会使用文本输入和模式属性.

点赞