Skip to content

cannotBeEmpty in configuration not working as expected #28827

Closed
@videni

Description

@videni

I explicitly set redirect_host can
NOT be empty, but when I try to validate this value , the $host variable with null value is passed.

 $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('app_url_shortener');

$rootNode
            ->children()
                ->scalarNode('redirect_host')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->validate()
                        ->ifTrue(function ($host) {
                            return preg_match('/https?/', $host);  //$host is null even the value is set
                        })
                        ->thenInvalid("You shouldn't specify protocol for 'redirect_host'")
                    ->end()
                ->end()
                ->scalarNode('provider')
                    ->isRequired()
                    ->cannotBeEmpty()
                    ->defaultValue('custom')
                    ->validate()
                        ->ifNotInArray(['custom', 'baidu'])
                        ->thenInvalid('The link provider %s is not supported')
                    ->end()
                ->end()
            ->end()
app_url_shortener:
    redirect_host: '%env(SHORT_LINK_REDIRECT_HOST)%'
    provider: 'baidu'

.env

SHORT_LINK_REDIRECT_HOST=example.com

ypeError: preg_match() expects parameter 2 to be string, null given in /Users/app/www/app/src/Bundle/UrlShortenerBundle/DependencyInjection/Configuration.php:30
Stack trace:
#0 /Users/app/www/app/src/Bundle/UrlShortenerBundle/DependencyInjection/Configuration.php(30): preg_match('/^https?/', NULL)
#1 /Users/app/www/app/vendor/symfony/config/Definition/Builder/ExprBuilder.php(242): App\Bundle\UrlShortenerBundle\DependencyInjection\Configuration->App\Bundle\UrlShortenerBundle\DependencyInjection{closure}(NULL)
#2 /Users/app/www/app/vendor/symfony/config/Definition/BaseNode.php(414): Symfony\Component\Config\Definition\Builder\ExprBuilder::Symfony\Component\Config\Definition\Builder{closure}(NULL)
#3 /Users/app/www/app/vendor/symfony/config/Definition/BaseNode.php(397): Symfony\Component\Config\Definition\BaseNode->finalize(NULL)
#4 /Users/app/www/app/vendor/symfony/config/Definition/ArrayNode.php(246): Symfony\Component\Config\Definition\BaseNode->finalize('env_43b19c36cb4...')
#5 /Users/app/www/app/vendor/symfony/config/Definition/BaseNode.php(408): Symfony\Component\Config\Definition\ArrayNode->finalizeValue(Array)
#6 /Users/app/www/app/vendor/symfony/config/Definition/Processor.php(39): Symfony\Component\Config\Definition\BaseNode->finalize(Array)
#7 /Users/app/www/app/vendor/symfony/config/Definition/Processor.php(52): Symfony\Component\Config\Definition\Processor->process(Object(Symfony\Component\Config\Definition\ArrayNode), Array)
#8 /Users/app/www/app/vendor/symfony/dependency-injection/Compiler/ValidateEnvPlaceholdersPass.php(80): Symfony\Component\Config\Definition\Processor->processConfiguration(Object(App\Bundle\UrlShortenerBundle\DependencyInjection\Configuration), Array)
#9 /Users/app/www/app/vendor/symfony/dependency-injection/Compiler/Compiler.php(95): Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass->process(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#10 /Users/app/www/app/vendor/symfony/dependency-injection/ContainerBuilder.php(746): Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object(Symfony\Component\DependencyInjection\ContainerBuilder))
#11 /Users/app/www/app/vendor/symfony/http-kernel/Kernel.php(515): Symfony\Component\DependencyInjection\ContainerBuilder->compile()
#12 /Users/app/www/app/vendor/symfony/http-kernel/Kernel.php(123): Symfony\Component\HttpKernel\Kernel->initializeContainer()
#13 /Users/app/www/app/vendor/lakion/api-test-case/src/ApiTestCase.php(74): Symfony\Component\HttpKernel\Kernel->boot()
#14 phar:///usr/local/Cellar/phpunit/7.3.1/bin/phpunit/phpunit/Framework/TestSuite.php(703): Lakion\ApiTestCase\ApiTestCase::createSharedKernel()
#15 phar:///usr/local/Cellar/phpunit/7.3.1/bin/phpunit/phpunit/Framework/TestSuite.php(750): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#16 phar:///usr/local/Cellar/phpunit/7.3.1/bin/phpunit/phpunit/TextUI/TestRunner.php(587): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
#17 phar:///usr/local/Cellar/phpunit/7.3.1/bin/phpunit/phpunit/TextUI/Command.php(203): PHPUnit\TextUI\TestRunner->doRun(Object(PHPUnit\Framework\TestSuite), Array, true)
#18 phar:///usr/local/Cellar/phpunit/7.3.1/bin/phpunit/phpunit/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#19 /usr/local/Cellar/phpunit/7.3.1/bin/phpunit(595): PHPUnit\TextUI\Command::main()
#20 {main}

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