【问题标题】:Symfony2 TreeBuilder configuration - validate against a set valueSymfony2 TreeBuilder 配置 - 根据设定值验证
【发布时间】:2013-02-14 10:28:10
【问题描述】:

我有以下捆绑配置:

    $supportedAdapters = array('curl', 'socket');

    $treeBuilder = new TreeBuilder();
    $rootNode = $treeBuilder->root('example_bundle');
    $rootNode
            ->children()
            ->scalarNode('username')->isRequired()->cannotBeEmpty()->end()
            ->scalarNode('password')->isRequired()->cannotBeEmpty()->end()
            ->scalarNode('adapter')
                ->validate()
                    ->ifNotInArray($supportedAdapters)
                    ->thenInvalid('The adapter %s is not supported. Please choose one of '.json_encode($supportedAdapters))
                ->end()
                ->cannotBeOverwritten()
                ->isRequired()
                ->cannotBeEmpty()
            ->end()
            // allow the use of a proxy for cURL requests
            ->arrayNode('proxy')
                ->children()
                    ->scalarNode('host')->isRequired()->cannotBeEmpty()->end()
                    ->scalarNode('port')->isRequired()->cannotBeEmpty()->end()
                    ->scalarNode('username')->defaultValue(null)->end()
                    ->scalarNode('password')->defaultValue(null)->end()
                ->end()
            ->end();

    return $treeBuilder;

我们支持两种适配器:curlsocket

我们仅支持对 curl 请求使用代理。在配置中,我想检查是否设置了代理并且适配器不是 curl,然后抛出错误通知用户“我们只支持 curl 适配器与代理一起使用”。有没有办法获得一个设定值(在我们的例子中是适配器)并检查它的值并针对它进行验证?

伪代码:

IF PROXY IS SET AND ADAPTER IS NOT EQUAL TO CURL THEN:
 THROW ERROR ("We don't support the use of a proxy with the socket adapter");
END IF;

我希望这是有道理的。我已经阅读了所有文档和 API 文档,但是,可惜我看不到实现这一点的选项。

【问题讨论】:

    标签: php symfony symfony-2.1


    【解决方案1】:

    解决了:

        $supportedAdapters = array('curl', 'socket');
    
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('example_bundle');
        $rootNode
                ->validate()
                    ->ifTrue(function($v){ return isset($v['proxy']) && 'curl' !== $v['adapter'];})
                    ->thenInvalid('Proxy support is only available to the curl adapter.')
                ->end()
                ->children()
                    ->scalarNode('username')->isRequired()->cannotBeEmpty()->end()
                    ->scalarNode('password')->isRequired()->cannotBeEmpty()->end()
                    ->scalarNode('adapter')
                        ->validate()
                            ->ifNotInArray($supportedAdapters)
                            ->thenInvalid('The adapter %s is not supported. Please choose one of '.json_encode($supportedAdapters))
                        ->end()
                        ->cannotBeOverwritten()
                        ->isRequired()
                        ->cannotBeEmpty()
                    ->end()
                    // allow the use of a proxy for cURL requests
                    ->arrayNode('proxy')
                        ->children()
                            ->scalarNode('host')->isRequired()->cannotBeEmpty()->end()
                            ->scalarNode('port')->isRequired()->cannotBeEmpty()->end()
                            ->scalarNode('username')->defaultValue(null)->end()
                            ->scalarNode('password')->defaultValue(null)->end()
                        ->end()
                ->end();
    
        return $treeBuilder;
    

    :-)

    【讨论】:

      猜你喜欢
      • 2012-11-29
      • 2018-01-12
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多