Error Call to a member function format () on string, In symfony form

-1

I have the following form in symfony:

->add('horaI', ChoiceType::class, array(
      'label' => 'Hora Inicio',
      'placeholder' => 'Seleccione una hora',
      'attr' => array(
                 'class' => 'form-name form-control'
                ),
      'choices'  => array(
                '08:00' => '8:00',
                '09:00' => '9:00',
                '10:00' => '10:00',
                '11:00' => '11:00',
                '12:00' => '12:00',
                '13:00' => '13:00',
                '14:00' => '14:00',
                '15:00' => '15:00',
                '16:00' => '16:00',
                '17:00' => '17:00',
)))

date is of time format, so when I save the form it shows me this error: Error Call to a member function format () on string.

I thought 2 solutions a datapicker with this bundle: link

But it does not only show the hours but also the headers of the month and year, besides hours that I do not need to take into account and that can cause the user to choose badly.

This is the problem. I'm working with symfony 3.2.x

Greetings

    
asked by juanitourquiza 14.06.2017 в 18:47
source

2 answers

1

The solution I found is each item to transform it to the datetime type according to an example of the symfony documentation: link

The code stayed as follows:

->add('horaI', ChoiceType::class, array(
            'label' => 'Hora Inicio',
            'placeholder' => 'Seleccione una hora',
            'attr' => array(
                    'class' => 'form-name form-control'
            ),
        'choices' => array(
    '08:00' => new \DateTime('08:00'),
    '09:00' => new \DateTime('09:00'),
    '10:00' => new \DateTime('10:00'),
    '11:00' => new \DateTime('11:00'),
    '12:00' => new \DateTime('12:00'),
    '13:00' => new \DateTime('13:00'),
    '14:00' => new \DateTime('14:00'),
    '15:00' => new \DateTime('15:00'),
    '16:00' => new \DateTime('16:00'),
    '17:00' => new \DateTime('17:00'),
)))

I hope you help them.

Greetings

    
answered by 14.06.2017 / 18:47
source
0

The solution to your problem may be using the TimeType provided by Symfony

link

    
answered by 28.02.2018 в 17:23