symfony/demo

Argument #1 ($string) must be of type string

Closed this issue · 9 comments

stof commented

This screenshot does not help, because it gives me a truncated info (I cannot scroll the page inside it or expand collapsible sections).
Please provide the textual representation of the stack trace instead (available in the Stack trace tab). And please actually provide it as text, not as a screenshot containing text.

Please also describe on which page you get this error (and which action you are doing) to allow reproducing the issue.

TypeError:
trim(): Argument #1 ($string) must be of type string, Symfony\Component\String\UnicodeString given

  at src/Form/DataTransformer/TagArrayToStringTransformer.php:89
  at trim()
     (src/Form/DataTransformer/TagArrayToStringTransformer.php:89)
  at App\Form\DataTransformer\TagArrayToStringTransformer->trim()
     (src/Form/DataTransformer/TagArrayToStringTransformer.php:57)
  at App\Form\DataTransformer\TagArrayToStringTransformer->reverseTransform()
     (vendor/symfony/form/Form.php:953)
  at Symfony\Component\Form\Form->normToModel()
     (vendor/symfony/form/Form.php:563)
  at Symfony\Component\Form\Form->submit()
     (vendor/symfony/form/Form.php:493)
  at Symfony\Component\Form\Form->submit()
     (vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:107)
  at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest()
     (vendor/symfony/form/Form.php:414)
  at Symfony\Component\Form\Form->handleRequest()
     (src/Controller/Admin/BlogController.php:88)
  at App\Controller\Admin\BlogController->new()
     (vendor/symfony/http-kernel/HttpKernel.php:183)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
     (vendor/symfony/http-kernel/HttpKernel.php:76)
  at Symfony\Component\HttpKernel\HttpKernel->handle()
     (vendor/symfony/http-kernel/Kernel.php:182)
  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:29)
  at require_once('/var/www/vhosts/mydomain.com/vendor/autoload_runtime.php')
     (public_html/index.php:5)         

`TypeError:
App\Entity\Post::setSlug(): Argument #1 ($slug) must be of type string, Symfony\Component\String\UnicodeString given, called in /var/www/vhosts/mydomain.com/src/Form/PostType.php on line 80

at src/Entity/Post.php:111
at App\Entity\Post->setSlug()
(src/Form/PostType.php:80)
at App\Form\PostType->App\Form{closure}()
(vendor/symfony/event-dispatcher/EventDispatcher.php:206)
at Symfony\Component\EventDispatcher\EventDispatcher->callListeners()
(vendor/symfony/event-dispatcher/EventDispatcher.php:56)
at Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
(vendor/symfony/event-dispatcher/ImmutableEventDispatcher.php:28)
at Symfony\Component\EventDispatcher\ImmutableEventDispatcher->dispatch()
(vendor/symfony/form/Form.php:558)
at Symfony\Component\Form\Form->submit()
(vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:107)
at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest()
(vendor/symfony/form/Form.php:414)
at Symfony\Component\Form\Form->handleRequest()
(src/Controller/Admin/BlogController.php:88)
at App\Controller\Admin\BlogController->new()
(vendor/symfony/http-kernel/HttpKernel.php:183)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw()
(vendor/symfony/http-kernel/HttpKernel.php:76)
at Symfony\Component\HttpKernel\HttpKernel->handle()
(vendor/symfony/http-kernel/Kernel.php:182)
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:29)
at require_once('/var/www/vhosts/mydomain.com/vendor/autoload_runtime.php')
(public_html/index.php:5)`

the problem is in this code:

               // if (null === $post->getSlug() && null !== $post->getTitle()) {
                //     $post->setSlug($this->slugger->slug($post->getTitle())->lower());
                // }

                if (empty($post->getSlug()) && !empty($post->getTitle())) {
                    $slug = $this->slugger->slug($post->getTitle())->lower();
                    $string = $slug->toString();
                    if (strlen($string) > 255) {
                        throw new \Exception('Generated slug exceeds the maximum length.');
                    }
                    $post->setSlug($string);
                }

from PostType.php file

and also in this code:

        // $names = array_filter(array_unique($this->trim(u($string)->split(','))));
        $names = array_filter(array_unique(array_map('trim', u($string)->split(','))));

from TagArrayToStringTransformer

fixed with chatGPT

stof commented

you still haven't answered my request of providing the reproducing steps

you still haven't answered my request of providing the reproducing steps

just try to add a post , in a server with php 8.4 and add for your php files strict_types

<?php declare(strict_types=1);

and add for your php files strict_types

Well, if you modify the code, you have of course make sure to do it consistently. Simply enabling strict types is not enough as you also may have to adjust some calls.

and add for your php files strict_types

Well, if you modify the code, you have of course make sure to do it consistently. Simply enabling strict types is not enough as you also may have to adjust some calls.

I enabled strict_types in all php files

I understood that and that’s simply not something you can do blindly without checking if the code contained in that file needs to be adapted.

I am closing here as that’s not an issue of the demo code but how you deal with it. Thank you for understanding.