Symfony3配置组件验证

亚历克斯

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

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

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

彼得

您可以通过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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章