2

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

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

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?

2
  • What exactly is not working with the given approach? What have you tried to debug the problem? Commented Jun 29, 2020 at 10:58
  • stackoverflow.com/questions/57385390 Commented Oct 14, 2020 at 14:11

3 Answers 3

0

You should move the html5 key/value from attr to options :

->add('date', DateTimeType::class, array(
'required' => true,
'widget' => 'single_text',
'html5' => false,
'attr' => [
    'class' => 'form-control input-inline datetimepicker',
    'data-provide' => 'datetimepicker',
],
))
Sign up to request clarification or add additional context in comments.

Comments

-1

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

The bootstrap approach expect a "normal" input field with a DateTime rendered as a string. So you have to use TextType::class To 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.

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 );
    }
) );

2 Comments

No, that's not needed - that's what the single_text option is set for
You have to use a TextType:::class to get the frontend part working with the bootstrap js, as the topic is called. When you want to store the value in DateTime-Type in the DB you have to transform the values. I can't see the point where this is not needed
-2

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

I hope this can help! good luck

1 Comment

No, that's not needed - that's what the single_text option is set for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.