Skip to content

NoSuchIndexException on DateType/TimeType fields when data class is null #47151

Closed
@trinko

Description

@trinko

Symfony version(s) affected

5.4

Description

I created a form without a Data Class, using fields with DateType or TimeType.
Submitting the form, this error is shown:
Cannot read index "xx" while trying to traverse path "[xx]". Available indices are "Array ...

The listed items of the array are only not DateType/ TimeType fields.

Note that in Symfony 4.4 I had no problems with it.
I think something is changed in form component, but I not found documentation about it.

How to reproduce

Simplified form example:
($option['dati'][1], $option['dati'][2], $option['dati'][3], $option['dati'][4] are all DateTime objects)

class ConfigurazioneType extends AbstractType {

  public function buildForm(FormBuilderInterface $builder, array $options) {
      $builder
        ->add('manutenzione', CheckboxType::class, array('label' => 'label.manutenzione_attiva',
          'data' => $options['dati'][0],
          'required' => false))
        ->add('data_inizio', DateType::class, array('label' => 'label.data_inizio',
          'data' => $options['dati'][1],
          'widget' => 'single_text',
          'html5' => false,
          'format' => 'dd/MM/yyyy',
          'required' => true))
        ->add('ora_inizio', TimeType::class, array('label' => 'label.ora_inizio',
          'data' => $options['dati'][2],
          'widget' => 'single_text',
          'html5' => false,
          'required' => true))
        ->add('data_fine', DateType::class, array('label' => 'label.data_fine',
          'data' => $options['dati'][3],
          'widget' => 'single_text',
          'html5' => false,
          'format' => 'dd/MM/yyyy',
          'required' => true))
        ->add('ora_fine', TimeType::class, array('label' => 'label.ora_fine',
          'data' => $options['dati'][4],
          'widget' => 'single_text',
          'html5' => false,
          'required' => true))
        ->add('submit', SubmitType::class, array('label' => 'label.submit'))
        ->add('cancel', ButtonType::class, array('label' => 'label.cancel'));
  }

  public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
      'data_class' => null));
  }

}


Possible Solution

The only way I found to avoid the error is to add a data transformer: DateTime to string. And then convert to DateTime in the controller.

This is the workaround:

  public function buildForm(FormBuilderInterface $builder, array $options) {
  ...
  
        $builder->get('data_inizio')
                     ->addModelTransformer(new CallbackTransformer(
                         function ($d) {
                             return $d; //no change: DateTime object
                         },
                         function ($d) {
                             return $d->format('d/m/Y');  // create a string
                         }
                     ))
                 ;

Additional Context

Stack Trace

Symfony\Component\PropertyAccess\Exception\NoSuchIndexException:
Cannot read index "data_inizio" while trying to traverse path "[data_inizio]". Available indices are "Array
(
    [0] => manutenzione
)
".

  at vendor/symfony/property-access/PropertyAccessor.php:377
  at Symfony\Component\PropertyAccess\PropertyAccessor->readPropertiesUntil()
     (vendor/symfony/property-access/PropertyAccessor.php:159)
  at Symfony\Component\PropertyAccess\PropertyAccessor->getValue()
     (vendor/symfony/form/Extension/Core/DataAccessor/PropertyPathAccessor.php:91)
  at Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor->getPropertyValue()
     (vendor/symfony/form/Extension/Core/DataAccessor/PropertyPathAccessor.php:61)
  at Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor->setValue()
     (vendor/symfony/form/Extension/Core/DataAccessor/ChainAccessor.php:54)
  at Symfony\Component\Form\Extension\Core\DataAccessor\ChainAccessor->setValue()
     (vendor/symfony/form/Extension/Core/DataMapper/DataMapper.php:87)
  at Symfony\Component\Form\Extension\Core\DataMapper\DataMapper->mapFormsToData()
     (vendor/symfony/form/Form.php:642)
  at Symfony\Component\Form\Form->submit()
     (vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:109)
  at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest()
     (vendor/symfony/form/Form.php:503)
  at Symfony\Component\Form\Form->handleRequest()
     (src/Controller/SistemaController.php:136)
  at App\Controller\SistemaController->manutenzioneAction()
     (vendor/symfony/http-kernel/HttpKernel.php:152)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (vendor/symfony/http-kernel/HttpKernel.php:74)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (vendor/symfony/http-kernel/Kernel.php:202)
  at Symfony\Component\HttpKernel\Kernel->handle()
     (vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35)
  at Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run()
     (vendor/autoload_runtime.php:35)
  at require_once('/var/www/giuaschool/vendor/autoload_runtime.php')
     (public/index.php:11)  

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions