Fabian
Fabian

Reputation: 1836

Symfony2: How to render datetime-local fields with FormBuilder?

The title says it all: How can i render html5 datetime-local fields from the formbuilder? With

$builder->add('start', 'datetime', array('widget' => 'single_text'))

i can get a datetime field, but this is not supported by chrome.

Thanks in advance!

Upvotes: 1

Views: 2369

Answers (2)

mhnrm
mhnrm

Reputation: 41

This is an old question, but for anybody coming across this post searching for a solution to that issue (like me), here is how I solved it:

Add a custom field type that inherits DateTimeType:

<?php

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

class LocalDateTimeType extends AbstractType
{
    const HTML5_FORMAT = "yyyy-MM-dd'T'HH:mm";

    /**
     * {@inheritdoc}
     */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['widget'] = $options['widget'];

        if ($options['html5'] && 'single_text' === $options['widget'] && self::HTML5_FORMAT === $options['format']) {
            $view->vars['type'] = 'datetime-local';
        }
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'format' => self::HTML5_FORMAT
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getParent()
    {
        return 'datetime';
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'local_datetime';
    }
}

Register it as a service:

app.form.type.local_datetime:
    class: AppBundle\Form\Type\LocalDateTimeType
    tags:
        - { name: form.type, alias: local_datetime }

Now you may use it in your forms:

(...)
$builder->add('myLocalDateTimeField', 'local_datetime', [
    'widget' => 'single_text'
];
(...)

It's important that you set the widget to single_text - and yes, this is of course possible (as of Symfony 2.7).

Using localized dateTimes, you'll probably want to store your datetimes normalized in the database. A nice approach can be found here:

http://jeremycook.ca/2012/06/24/normalising-datetimes-with-doctrine-events/

Upvotes: 4

Rooneyl
Rooneyl

Reputation: 7902

The widget 'single_text' is not supported by the datetime field type.

The date and time parts have independent widgets. For Example:

<?php
// from date
        $builder->add('DateFrom', 'datetime', array(
            'input'  => 'datetime',
            'date_widget' => 'single_text',
            'time_widget' => 'single_text',
            'empty_value' => '',
            'format' => 'yyyy-MM-dd',
            'label' => 'From Date',
            'required' => false,
            'mapped' => false
        ));

Definition of the widget options for the date type.
Definition of the widget options for the time type.

Upvotes: 0

Related Questions