Symfony3 配置组件验证

Symfony3 configuration component validation

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

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

如果 arrayNode 仅包含 2 个允许的数组之一,我如何使用 TreeBuilder 进行验证:allowed_countriesdisallowed_countries 两个数组在一起抛出异常? Symfony 版本 3.2

您可以使用 validation rules with the ExprBuilder.

向配置树构建器添加更复杂的验证

这看起来像:

$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();