Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to globally set date format for Symfony forms?

What's the correct way to globally set custom date format for Symfony2 forms?

Default format is like Y-m-d, and if I enter date like m/d/Y or d/m/Y it's treated as invalid.

Is there a way to somehow relate format to selected locale? I'm looking for simple solution without writing custom form types.

like image 616
Johnny Avatar asked Dec 29 '14 12:12

Johnny


3 Answers

You can set the format in your FormType if you want another format.

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

The default it not 'Y-m-d' its an instance of IntlDateFormatter. So that should select the correct locale if you don't use single text.

type: integer or string default: IntlDateFormatter::MEDIUM (or yyyy-MM-dd if widget is single_text)

http://symfony.com/doc/current/reference/forms/types/date.html#format http://php.net/manual/en/class.intldateformatter.php

like image 174
René Höhle Avatar answered Nov 18 '22 15:11

René Höhle


You should be able to set this globally using an Event Listener http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

like image 39
AndrewVT Avatar answered Nov 18 '22 16:11

AndrewVT


If you want to change the format of your date field depending on the locale, you'll have to create a custom field and use it instead of date. It's the only way that I'm aware of.

First, create the service and pass the locale in the arguments:

# src/Acme/DemoBundle/Resources/config/services.yml
services:
    acme_demo.form.type.localedate:
        class: Acme\DemoBundle\Form\Type\LocaleDateType
        arguments:
            - "%locale%"
        tags:
            - { name: form.type, alias: localedate }

Create the class with the code for the new field type. As you can see, it's based on the field type date and changes the format depending on the current locale:

// src/Acme/DemoBundle/Form/Type/LocaleDateType.php
namespace Acme\DemoBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class LocaleDateType extends AbstractType
{
    private $locale;

    public function __construct($locale)
    {
        $this->locale = $locale;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'format' => $this->getFormat(),// return the format depending on locale
        ));
    }

    private function getFormat() {
        switch($this->locale) {
            case 'es':
                return 'dd-MM-yyyy';// return valid date format for this locale
            default:
                return 'yyyy-MM-dd';
        }
    }

    public function getParent()
    {
        return 'date';
    }

    public function getName()
    {
        return 'localedate';
    }
}

Use it in your application instead of the date field type:

$builder->add('date_created', 'localedate', array(
    'widget' => 'single_text',
));

Hope it helps.

Kind regards.

like image 1
acontell Avatar answered Nov 18 '22 14:11

acontell