MSK90
MSK90

Reputation: 151

How to display a datepicker on Symfony?

I want to set up a date-picker for my date type fields based on symfony documentation. I have installed the package since packagist 'eternicode / bootstrap-datepicker'. I am able to apply my defined conditions for entering the date field in my js (like the date format) but the display is still impossible. I do not have an error message in my js console. I don't even see the datepikcer component..

My Form Type:

   ->add('date_entree', DateType::class, [ 
                'widget' => 'single_text',
                          'html5' => false,
                          'attr' => ['class' => 'js-datepicker'],
                ])

My JS:

    $(document).on('ready', function() {
         $('.js-datepicker').datepicker({
              format: 'dd-mm-yyyy',
         });
     });

Have you please an idea about this issue?

Upvotes: 4

Views: 9349

Answers (1)

MSK90
MSK90

Reputation: 151

Thank you for your help.

Finally, this was my solution:

  • css:

     < link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.css" rel="stylesheet"/>
    
  • JS

            <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.js"></script>
        <script>
            $(document).ready(function() {
                // you may need to change this code if you are not using Bootstrap Datepicker
                $('.js-datepicker').datepicker({
                    format: 'yyyy-mm-dd'
                });
            });
        </script>
    
  • Form Type

       ->add('date_entree', DateType::class, [ 
            'widget' => 'single_text',
                      'html5' => false,
                      'attr' => ['class' => 'js-datepicker'],
            ])
    

Hope that will be helpful for others developers!

Upvotes: 11

Related Questions