Sonataadminbundle: TypeError htmlspecialchars() expects parameter 1 to be string, null given

Created on 2 Apr 2019  路  5Comments  路  Source: sonata-project/SonataAdminBundle

Environment

Development

Sonata packages

$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle              3.48.0 3.48.0 The missing Symfony A...
sonata-project/block-bundle              3.15.0 3.15.0 Symfony SonataBlockBu...
sonata-project/cache                     2.0.1  2.0.1  Cache library
sonata-project/core-bundle               3.16.2 3.16.2 Symfony SonataCoreBundle
sonata-project/datagrid-bundle           2.5.0  3.0.0  Symfony SonataDatagri...
sonata-project/doctrine-extensions       1.2.0  1.2.0  Doctrine2 behavioral ...
sonata-project/doctrine-orm-admin-bundle 3.8.3  3.8.3  Symfony Sonata / Inte...
sonata-project/easy-extends-bundle       2.5.0  2.5.0  Symfony SonataEasyExt...
sonata-project/exporter                  1.11.0 2.0.1  Lightweight Exporter ...
sonata-project/formatter-bundle          4.1.2  4.1.2  Symfony SonataFormatt...
sonata-project/intl-bundle               2.5.0  2.5.0  Symfony SonataIntlBundle
sonata-project/user-bundle               4.3.0  4.3.0  Symfony SonataUserBundle

Symfony packages

$ composer show --latest 'symfony/*'
symfony/contracts          v1.0.2  v1.0.2  A set of abstractions extracted o...
symfony/maker-bundle       v1.9.0  v1.11.5 Symfony Maker helps you create em...
symfony/monolog-bundle     v3.3.1  v3.3.1  Symfony MonologBundle
symfony/polyfill-ctype     v1.11.0 v1.11.0 Symfony polyfill for ctype functions
symfony/polyfill-iconv     v1.11.0 v1.11.0 Symfony polyfill for the Iconv ex...
symfony/polyfill-intl-icu  v1.11.0 v1.11.0 Symfony polyfill for intl's ICU-r...
symfony/polyfill-intl-idn  v1.11.0 v1.11.0 Symfony polyfill for intl's idn_t...
symfony/polyfill-mbstring  v1.11.0 v1.11.0 Symfony polyfill for the Mbstring...
symfony/polyfill-php56     v1.11.0 v1.11.0 Symfony polyfill backporting some...
symfony/polyfill-php70     v1.11.0 v1.11.0 Symfony polyfill backporting some...
symfony/polyfill-php72     v1.11.0 v1.11.0 Symfony polyfill backporting some...
symfony/polyfill-util      v1.11.0 v1.11.0 Symfony utilities for portability...
symfony/security-acl       v3.0.1  v3.0.1  Symfony Security Component - ACL ...
symfony/swiftmailer-bundle v3.2.5  v3.2.5  Symfony SwiftmailerBundle
symfony/symfony            v4.2.4  v4.2.4  The Symfony PHP framework

PHP version

$ php -v
PHP 7.2.4 (cli) (built: Apr  5 2018 00:37:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.4, Copyright (c) 1999-2018, by Zend Technologies

Subject

Running a composer update on my project produces the following error when I add a space in a required text form field and submit the form:

TypeError
HTTP 500 Internal Server Error
htmlspecialchars() expects parameter 1 to be string, null given

Before the composer update I was using sonata-project/admin-bundle 3.43.0. I experimented with downgrading from 3.48.0 to 3.43.0 and was able to determine that the issue was introduce in 3.44.0.

Steps to reproduce

  1. Log into to Sonata Admin
  2. Add or edit an available entity
  3. Enter a space (space bar) in a required text field.
  4. Submit the form.

Expected results

  1. A flash message appears above the form with the message
An error has occurred during the creation of item "". 
  1. The violating field is highlighted in red with an ! beneath it with a message This value should not be blank.

Actual results


Symfony Exception
Symfony Docs
Symfony Support
TypeError
HTTP 500 Internal Server Error
htmlspecialchars() expects parameter 1 to be string, null given

    Exception Logs 2 Stack Trace 

TypeError
in vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php (line 1444)

         *
         * @return string
         */
        protected function escapeHtml($s)
        {
            return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
        }
        /**
         * Get CSRF token.
         *

in vendor/sonata-project/admin-bundle/src/Controller/CRUDController.phphtmlspecialchars (line 1444)

         *
         * @return string
         */
        protected function escapeHtml($s)
        {
            return htmlspecialchars($s, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
        }
        /**
         * Get CSRF token.
         *

in vendor/sonata-project/admin-bundle/src/Controller/CRUDController.php->escapeHtml (line 396)

                    if (!$this->isXmlHttpRequest()) {
                        $this->addFlash(
                            'sonata_flash_error',
                            $this->trans(
                                'flash_edit_error',
                                ['%name%' => $this->escapeHtml($this->admin->toString($existingObject))],
                                'SonataAdminBundle'
                            )
                        );
                    }
                } elseif ($this->isPreviewRequested()) {

in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php->editAction (line 150)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php->handleRaw (line 67)
in vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php->handle (line 198)
Kernel->handle(object(Request)) in web/app_dev.php (line 12)
500

bug unconfirmed

Most helpful comment

Added a PR #5516 - was able to fix it everywhere with a single line edit.

All 5 comments

I suspect this has to do with the declare(strict_types=1); statements added in commit e1d07271c84f1c980dc83b7697353dd39846411f since the error is a TypeError.

Yes, this is a bit stricter now. You can fix it by making sure $this->admin->toString() always return a string. Did you override AbstractAdmin::toString, BTW?

I did indeed override the AbstractAdmin::toString for all my Admin classes. Casting the return as a string fixes this issue. Sorry I guess this isn't a bug since I can fix it within my function overrides, but there will likely be others that will run into this issue.

Yes, indeed, if you want to improve things for others, you could create a PR that adds (string) before all calls to escapeHtml in the codebase.

Added a PR #5516 - was able to fix it everywhere with a single line edit.

Was this page helpful?
0 / 5 - 0 ratings