twitter-bootstrap 如何以 Symfony 形式添加 bootstrap datetimepicker

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41781479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:32:33  来源:igfitidea点击:

How to add bootstrap datetimepicker in Symfony form

twitter-bootstrapsymfonydatepicker

提问by Steven Seagull

I'm trying to use http://eonasdan.github.io/bootstrap-datetimepicker/in my Symfony form.

我正在尝试在我的 Symfony 表单中使用http://eonasdan.github.io/bootstrap-datetimepicker/

I added:

我补充说:

<link type="text/css" rel="stylesheet" href="{{ asset('assets/css/bootstrap-datetimepicker-standalone.min.css') }}">
<link type="text/css" rel="stylesheet" href="{{ asset('assets/css/bootstrap.min.css') }}">

<script type="text/javascript" charset="utf8" src="{{ asset('assets/js/jquery-3.1.1.min.js') }}"></script>
<script type="text/javascript" charset="utf8" src="{{ asset('assets/js/moment.min.js') }}"></script>
<script type="text/javascript" charset="utf8" src="{{ asset('assets/js/bootstrap.min.js') }}"></script>
<script type="text/javascript" charset="utf8" src="{{ asset('assets/js/bootstrap-datetimepicker.min.js') }}"></script>

But now I'm not sure how do I change my form type to use datepicker

但是现在我不确定如何更改表单类型以使用 datepicker

I tried something like this and it's not working

我试过这样的事情,但它不起作用

->add('date', DateTimeType::class, array(
'required' => true,
'widget' => 'single_text',
'attr' => [
    'class' => 'form-control input-inline datetimepicker',
    'data-provide' => 'datetimepicker',
    'html5' => false,
],
))

Can someone help me?

有人能帮我吗?

回答by Sma?ne

Assume the doc http://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-textbox. Your code seem's good. Do you have any error in the console ?

假设文档http://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-textbox。你的代码看起来不错。您在控制台中有任何错误吗?

回答by Hamid ER-REMLI

I'm not sure it is the unique issue, but you might need to change DateTimeType::class to texttype::class.

我不确定这是唯一的问题,但您可能需要将 DateTimeType::class 更改为 texttype::class。

I hope this can help! good luck

我希望这会有所帮助!祝你好运

回答by Matthias Tosch

I was facing the same problem. The default Form-Component from Symfony for Datetime type render an other set of input fields.

我面临着同样的问题。Symfony 中用于 Datetime 类型的默认 Form-Component 呈现另一组输入字段。

The bootstrap approach expect a "normal" input field with a DateTime rendered as a string. So you have to use TextType::classTo work with a DateTime in the Entity i've added an EntityTransformer to handle the conversion between sting and DateTime for read and write operations.

bootstrap 方法需要一个“普通”输入字段,其中 DateTime 呈现为字符串。因此,您必须使用TextType::class来处理实体中的 DateTime,我添加了一个 EntityTransformer 来处理 sting 和 DateTime 之间的转换以进行读写操作。

you can do it like this:

你可以这样做:

$builder->get( 'from' )->addModelTransformer( new CallbackTransformer(
    function ( $date ) {
        return $date->format( 'd.m.Y H:i' );
    },
    function ( $date ) {
        return new \DateTime( $date );
    }
) );