Symfony3配置组件验证

时间:2018-07-28 20:43:45

标签: symfony symfony-config-component

社区,我需要您的帮助。我有配置文件:

payments:
    methods:
        paypal:
           enabled: false
           allowed_countries:
              - <country>
              - ...
        credit_card:
           disallowed_countries:
              - <country>
              - ...

如果arrayNode仅包含两个允许的数组之一,如何使用TreeBuilder进行验证: allowed_countries disallowed_countries 并抛出异常,如果同时存在两个数组? Symfony 3.2版

1 个答案:

答案 0 :(得分:0)

通过将validation rulesExprBuilder结合使用,可以向配置树构建器添加更复杂的验证。

这看起来像:

$rootNode
    ->isRequired()
    ->validate()
        ->ifTrue(function($options) {
            return !($options['allowed_countries'] xor $options['disallowed_countries']);
        })
        ->thenInvalid('Either define allowed_countries or disallowed_countries, not both')
        ->end()
    ->children()
        ->arrayNode('allowed_countries')
            ->scalarPrototype()->end()
        ->end()
        ->arrayNode('disallowed_countries')
            ->scalarPrototype()->end()
        ->end()
    ->end();
相关问题