【问题标题】:Symfony3 configuration component validationSymfony3 配置组件验证
【发布时间】:2019-01-05 14:12:20
【问题描述】:

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

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

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

【问题讨论】:

  • 请出示您的 ConifgTreeBuilder

标签: symfony symfony-config-component


【解决方案1】:

您可以使用 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();

【讨论】:

    猜你喜欢
    • 2016-10-16
    • 2016-09-30
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    相关资源
    最近更新 更多