Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[Form] `datetime` form field type cannot accept DateTime as a default value #3288

Closed
zerkms opened this Issue Feb 6, 2012 · 6 comments

Comments

Projects
None yet
5 participants
Contributor

zerkms commented Feb 6, 2012

Steps to reproduce:

  1. Create new form type class like:

    class HistorySliderType extends AbstractType
    {
        /**
         * {@inheritdoc}
         */
        public function buildForm(FormBuilder $builder, array $options)
        {
            $datetime = new \DateTime();
    
            $builder
                ->add('date', 'datetime', array(
                    'date_widget' => 'single_text',
                    'time_widget' => 'single_text',
                    'date_format' => 'yyyy-MM-dd',
                    'with_seconds' => true,
                    'data' => $datetime
                ));
        }
    
        /**
         * {@inheritdoc}
         */
        function getName()
        {
            return 'acme_tbundle_historyslidertype';
        }
    }
    
  2. Render the form

The result: Expected argument of type "DateTime", "array" given

@fabpot fabpot added a commit that referenced this issue Feb 10, 2012

@fabpot fabpot merged branch bschussek/issue3288 (PR #3290)
Commits
-------

22c8f80 [Form] Fixed issues mentioned in the PR comments
3b1b570 [Form] Fixed: The "date", "time" and "datetime" types can be initialized with \DateTime objects
88ef52d [Form] Improved FormType::getDefaultOptions() to see default options defined in parent types
b9facfc [Form] Removed undefined variables in exception constructor

Discussion
----------

[Form] Fixed: "date", "time" and "datetime" fields can be initialized with \DateTime objects

Bug fix: yes
Feature addition: yes
Backwards compatibility break: **yes**
Symfony2 tests pass: yes
Fixes the following tickets: #3288
Todo: -

![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=issue3288)

Fixed exception that was thrown when doing:

    $builder->add('createdAt', 'date', array('data' => new \DateTime()));

On a side note, the options passed to `FieldType::getDefaultOptions` now always also contain the default options of any parent types. This is necessary if you want to be independent of how `getDefaultOptions` is implemented in the parent type and still rely on the already defined values.

As a result, `FieldType::getParent` doesn't see default options anymore. This shouldn't be a big problem, because this method only relies on options in few cases. If it does, options now need to be checked for existence with `isset` before being used (BC break).

---------------------------------------------------------------------------

by bschussek at 2012-02-09T16:14:46Z

@fabpot Ready to merge.

---------------------------------------------------------------------------

by bschussek at 2012-02-10T12:15:04Z

@fabpot Ready to merge
be2e67c

@fabpot fabpot closed this Feb 10, 2012

@Burgov Burgov added a commit to Burgov/symfony that referenced this issue Mar 31, 2012

@Burgov Burgov [Form] Wrote a test to assure #3288 is still fixed 49afcba
Contributor

mikeSimonson commented May 8, 2012

Hi,

That this issue is still there in the 2.0.13 version (tested this morning).
Is there a reason for the fix to have been removed ?

Thanks mike

mackram commented May 15, 2012

Hi,

I like mike cant find the correction of this issue in neither version 2.0.13 nor 2.0.12. I have checked both my local pulled version as well as the one on github and for some reason it seems that the corrections were removed. Anyone have any points on why this was done

Contributor

zerkms commented May 15, 2012

@mikeSimonson let's reopen it then?

UPD: oops, don't have permissions to reopen :-(

Member

stof commented May 15, 2012

The fix has never been part of the 2.0.x branch. It has been fixed in master during one of the Form changes

mackram commented May 15, 2012

I see, it is surprising to me though on why it never made it to the 2.0.x branch since the error is quite a stopper on anyone with a form with datetime objects (in say a database) and it does not seem to be causing issues elsewhere. Any chance someone can explain this to me.

Member

stof commented May 15, 2012

@mackram because we cannot merge the BC breaking changes of the Form component into 2.0.x (a maintenance release is not allowed to break BC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment