8000 NoSuchIndexException on DateType/TimeType fields when data class is null · Issue #47151 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trinko opened this issue Aug 2, 2022 · 8 comments
Closed

Comments

@trinko
Copy link
trinko commented Aug 2, 2022

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)  
@xabbuh
Copy link
Member
xabbuh commented Aug 2, 2022

Can you create a small example application that allows to reproduce your issue?

@trinko
Copy link
Author
trinko commented Aug 2, 2022

I attached the application files: controller, form type and twig template.

bug.zip

I also attached the patched version.
patched.zip

@xabbuh
Copy link
Member
xabbuh commented Aug 2, 2022

I cannot reproduce your issue. Using your code from the bug.zip I can successfully submit the form and the submitted data is output correctly.

@trinko
Copy link
Author
trinko commented Aug 3, 2022

I found the problem on Symfony 5.4.10. I updated to 5.4.11 and the error is always there.

I can attach the relevant config files, maybe there is some difference on form/validation settings.

config.zip

@xabbuh
Copy link
Member
xabbuh commented Aug 3, 2022

Can you push a complete application that I can check out and just execute? There may be other subtle differences.

@trinko
Copy link
Author
trinko commented Aug 4, 2022

Ok, I attached a full application. Run composer to install Symfony 5.4
bug.zip

Anyway: if you disable in configuration the flag...

  property_access:
      throw_exception_on_invalid_index: false

you will have no error, the datetime field is read without problems. No data lost.
If the flag is active, you have the error, as the datetime fields are lost.

@xabbuh
Copy link
Member
xabbuh commented Aug 4, 2022

Status: Reviewed

@xabbuh
Copy link
Member
xabbuh commented Aug 5, 2022

will be fixed by #47200

@fabpot fabpot closed this as completed Aug 5, 2022
fabpot added a commit that referenced this issue Aug 5, 2022
…o uninitialized arrays (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Form] ignore missing keys when mapping DateTime objects to uninitialized arrays

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #47151
| License       | MIT
| Doc PR        |

Commits
-------

6d79f68 ignore missing keys when mapping DateTime objects to uninitialized arrays
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0