1

Based on this question I'm trying to configure a filter date, but the default format is for example "May 13, 2016 1:12 PM 13, 2016, 1:12:50 pm". In my DB I store datetimes in this format "aaaa-mm-dd hh:mm:ss". So, when I apply the filter above, none result is returned.

My code:

//Admin Class.php   
 protected function configureDatagridFilters(DatagridMapper $datagridMapper)
        {
            $datagridMapper
                ->add('firstName')
                ->add('lastName')
                ->add('createdAt', 'doctrine_orm_datetime_range', array(
                    'field_type' => 'sonata_type_datetime_range_picker',
                    'show_filter' => true
                ))
            ;
        }

#config.yml
sonata_admin:
    templates:
        layout: AppBundle::standard_layout.html.twig

Trying something like this, but does not work.


{# standard_layout.html.twig #}
{% extends 'SonataAdminBundle::standard_layout.html.twig' %}

{% block javascripts %}
    {{ parent() }}
    jQuery(document).ready(function(){
        //jQuery.datepicker.setDefaults( jQuery.datepicker.regional[ "" ] );
        $('#dtp_filter_createdAt_value_start').datetimepicker({
            dateFormat: "yy-mm-dd"
        });
    });
{% endblock %}
Community
  • 1
  • 1
Luca Perico
  • 1,335
  • 15
  • 29

3 Answers3

1

I had a similar problem but got it working fine when I added the format parameter as shown here...

$datagridMapper...

->add('dateProcessed', 'doctrine_orm_datetime', 
       array('field_type' =>'sonata_type_datetime_picker'), null, array(
       'dp_use_current'=> true,
       'format'=>'dd/MM/yyyy'))

You don't say if you are really asking for a date range picker or just a selection on a single date but the code here worked for me when just performing a filter on a single date. I think you could still use the same approach for the range if required.

1

The way to do it, at least with doctrine-orm-admin-bundle v3.0.5:

->add('createdAt', 'doctrine_orm_datetime_range', array(
    'label' => 'Registered at',
        ), null, array(
    'field_type' => 'sonata_type_date_picker',
    'field_options' => array(
        'format' => 'dd/MM/yyyy'
    ),
    'translation_domain' => 'SonataAdminBundle')
)
devilcius
  • 1,764
  • 14
  • 18
1

This should work for you:

$datagridMapper
    ->add('createdAt', 'doctrine_orm_date_range', array(), 
              'sonata_type_date_range_picker'
            , array('field_options_start' => array('format' => 
              'yyyy-MM-dd'),
            'field_options_end' => array('format' => 'yyyy-MM-dd'))
    )
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Saviour Dela
  • 139
  • 1
  • 7