Sonataadminbundle: Eonasdan datetimepicker - date range format DD/MM/YYYY

Created on 6 May 2016  路  7Comments  路  Source: sonata-project/SonataAdminBundle

Hi!

| Bundle | Version |
| --- | --- |
| sonata-project/admin-bundle | 2.3.10 |
| sonata-project/core-bundle | v2.8.4 |
| sonata-project/doctrine-orm-admin-bundle | 2.3.3 |
| symfony/symfony | v2.8.4 |
| sonata-project/intl-bundle | 2.2.4 |

I'm using sandbox.

I'm trying to set default date filter to the pattern DD/MM/YYYY or even DD/MM/YY.

My locale is pt_BR. Eonasdan datetimepicker translation didn't work automatically so I manually added the pt-br.js

I was trying to set default format it in some config file to not need to add in each time that I use a date filter but first I've tried to add in the field set:

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('updatedAt', 'doctrine_orm_date_range', array(
                'field_type' => 'sonata_type_date_range_picker',
                'field_options' => array('format' => \IntlDateFormatter::SHORT)
            ));
    }

But using this way just current day is enable under datepicker, don't know why. In html:

data-date-format="DD/MM/YY"

Forcing yy => YYYY in line 32 of MomentFormatConverter.php

The html turns to:

data-date-format="DD/MM/YYYY"

And the datepicker almost works, all days are enabled, I can select, filter but after filter I can't change the date, the other days are disabled.

I also tried format like this:

        $datagridMapper
            ->add('updatedAt', 'doctrine_orm_date_range', array(
                'field_type' => 'sonata_type_date_range_picker',
                'field_options' => array('format' => 'DD/MM/YYYY')
            ));

But it throws:

The "format" option should contain the letters "y", "M" and "d". Its current value is "DD/MM/YYYY".

in vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/DateType.php at line 60

Most helpful comment

You can use it
->add('created','doctrine_orm_date_range', array('show_filter' => true),'sonata_type_date_range_picker',
array('field_options_start' => array('format' => 'yyyy-MM-dd'),
'field_options_end' => array('format' => 'yyyy-MM-dd'))
);

All 7 comments

Check this line. Change 'format' => 'DD/MM/YYYY' to 'date_format' => 'DD/MM/YYYY'. See if it works.

No chances...

The option "date_format" does not exist. Defined options are: "action", "allow_extra_fields", "attr", 
"auto_initialize", "block_name", "by_reference", "cascade_validation", "choice_translation_domain", 
"compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_provider", 
"csrf_token_id", "csrf_token_manager", "data", "data_class", "datepicker", "datepicker_use_button", 
"days", "description", "disabled", "dp_days_of_week_disabled", "dp_default_date", 
"dp_disabled_dates", "dp_enabled_dates", "dp_icons", "dp_language", "dp_max_date", "dp_min_date", 
"dp_pick_time", "dp_show_today", "dp_side_by_side", "dp_use_current", "dp_use_strict", 
"empty_data", "empty_value", "error_bubbling", "error_delay", "error_mapping", "error_type", 
"extra_fields_message", "format", "help_block", "help_label", "help_label_popover", 
"help_label_tooltip", "help_widget_popover", "horizontal", "horizontal_input_wrapper_class", 
"horizontal_label_class", "horizontal_label_offset_class", "html5", "inherit_data", "inline", "input", 
"intention", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", 
"label_render", "mapped", "max_length", "method", "model_timezone", "months", 
"omit_collection_item", "pattern", "placeholder", "post_max_size_message", "property_path", 
"read_only", "render_fieldset", "render_optional_text", "render_required_asterisk", "required", 
"show_child_legend", "show_legend", "sonata_admin", "sonata_field_description", "sonata_help", 
"tabs_class", "translation_domain", "trim", "validation_groups", "view_timezone", "virtual", "widget", 
"widget_add_btn", "widget_addon_append", "widget_addon_prepend", "widget_checkbox_label", 
"widget_form_group", "widget_form_group_attr", "widget_items_attr", "widget_prefix", 
"widget_remove_btn", "widget_suffix", "widget_type", "years".

It was my mistake. The options is format. But I'm looking here and I can't find a filter for sonata_type_date_range_picker. What if you change the field_type option to sonata_type_date_range? The error still there? Cause I think that there are no Type for a data_range_picker.

sonata_type_date_range_picker https://github.com/sonata-project/SonataCoreBundle/blob/d02a84c0b39e573541bab277515387399d4ef828/Form/Type/DateRangePickerType.php#L41

I would like to use Eonasdan datetimepicker so it need to be sonata_type_date_range_picker using sonata_type_date_range as field_type will add select2 with day, month and year separately.

Ok, this is what I think is the problem. You are trying to use DateRangePickerType, ok. But if you check this folder, you will see that the types to use with the datagridMapper are different form the used in the formMapper. IMO that is the problem. If this is not, then I'm out of ideas.

You can use it
->add('created','doctrine_orm_date_range', array('show_filter' => true),'sonata_type_date_range_picker',
array('field_options_start' => array('format' => 'yyyy-MM-dd'),
'field_options_end' => array('format' => 'yyyy-MM-dd'))
);

Thanks @ArturTovmasyan, it worked! Just changed for my country date pattern dd/MM/yyyy

Was this page helpful?
0 / 5 - 0 ratings