1

The default datetime format in a symfony datetime field is yyyy-MM-dd HH:mm:ss for my locale. This is not very user friendly so I'd like to change it to the long date format. I'm also using jquery datetimepicker after the page loads to assist users in choosing the correct date/time.

I can't seem to get the date formats to match up and have symfony consider the data that's passed back as a valid date.

Controller code for the form:

$form = $this->createFormBuilder($auction)
        ->add('endDate', DateTimeType::class, array(
                'view_timezone' => $user->getTimeZone(),
                'widget' => 'single_text',
                'format' => \IntlDateFormatter::FULL
            ))
        ->add('save', SubmitType::class, array('label' => 'Create Auction', 'attr'=> array('class'=>'button primary')))
        ->getForm();

View code for the datetimepicker:

$(document).ready(function(){
        $.datetimepicker.setDateFormatter({
            parseDate: function(date, format){
                var d = moment(date, format);
                return d.isValid() ? d.toDate() : false;
            },
            formatDate: function(date, format){
                return moment(date).format(format);
            }
        });
        $("#form_endDate").datetimepicker({
            format: 'dddd, MMMM D, YYYY h:mm:ssa',
            formatDate: 'dddd MMMM D, YYYY',
            formatTime: 'h:mm a'
        });
    });

I've also tried using the same text in the Controller to match the format in the datetimepicker initialization:

'format' => 'dddd, MMMM D, YYYY h:mm:ssa'

But that does not help. There is nothing in the logs either so I don't know how else to troubleshoot.

How do I get these formats to match so that the user entered input is valid?

John the Ripper
  • 2,389
  • 4
  • 35
  • 61

1 Answers1

0

Your format, in your formbuilder is incorrect. As per the docs: http://symfony.com/doc/current/reference/forms/types/date.html#format

The format should be specified like this:

$builder->add('date_created', DateType::class, array(
    'widget' => 'single_text',
    // this is actually the default format for single_text
    'format' => 'yyyy-MM-dd',
));

You can view all the valid formats here: http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax

Daniel Mensing
  • 956
  • 5
  • 13
  • Actually the link you provided does in fact mention that I should be able to use the \IntlDateFormatter constants. – John the Ripper Aug 05 '16 at 17:19
  • Oh yeah, didnt notice. My bad. Does the initial date showed in your input have the same date format as the one generated by datetimepicker? You could also examine the posted data before submitting it in your controller, to make sure it has the correct date format. – Daniel Mensing Aug 08 '16 at 07:18