【问题标题】:Configuration tree : add conditional validation配置树:添加条件验证
【发布时间】:2013-11-03 11:12:10
【问题描述】:

我正在尝试创建一个包来管理可以通过配置文件配置的菜单。

所以我在class DependencyInjection\Configuration 中写了一些配置约束。

所需的配置是必须添加到菜单中的项目数组。每个项目可以有 3 种不同的类型(linklink_notificationwidget)。并且对于每种类型,item 都需要其他属性(如routelabel 等)。

配置示例:

menu:
    utilities:
        - { type: link, icon: icon_name, label: text, route: { name: route_name, params: {} } }
        - { type: link_notification, notification: notification_text }
        - { type: widget, controller: controller_name }

我被困住了,因为我找不到如何为每种类型定义不同的数组约束。

我找不到翻译条件的方法:

IF type == "link" THEN scalarNode "icon" IS REQUIRED AND scalarNode "label" IS REQUIRED ...

配置文件如下所示:

public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('aiomedia_menu');

        $rootNode
            ->children()
                ->arrayNode('utilities')
                    ->prototype('array')
                        ->children()
                            ->enumNode('type')
                                ->values(array ('link', 'link_notification', 'widget'))
                                ->isRequired()
                            ->end()
                        ->end()
                    ->end()
                ->end()
            ->end();

        return $treeBuilder;
    }

我在 Symfony2 文档中看到了方法 ->ifXXX() [...] ->then(),但我不知道如何在这种情况下使用它们。

【问题讨论】:

    标签: symfony configuration tree


    【解决方案1】:

    类似于:(未经测试,但它为您提供了一些寻找方向)

    $rootNode
        ->children()
            ->arrayNode('utilities')
                ->prototype('array')
                    ->validation()
                        ->ifTrue(function ($v) {
                            if (!is_array($v)) {
                                return true;
                            }
    
                            switch ($v['type']) {
                                case 'link':
                                    $requiredSettings = array('icon', 'label', 'route');
                                    break;
    
                                case 'link_notification':
                                    $requiredSettings = array('notification');
                                    break;
    
                                case 'widget':
                                    $requiredSettings = array('controller');
                                    break;
                            }
    
                            foreach ($requiredSettings as $setting) {
                                if (!array_key_exists($setting, $v)) {
                                    return false;
                                }
                            }
    
                            return true;
                        })
                        ->thenInvalid('Missing required options for "%s"')
                    ->end()
                    ->children()
                        ->enumNode('type')
                            ->values(array ('link', 'link_notification', 'widget'))
                            ->isRequired()
                        ->end()
                    ->end()
                ->end()
            ->end()
        ->end();
    

    【讨论】:

      猜你喜欢
      • 2016-06-03
      • 2017-08-07
      • 2013-05-24
      • 2018-03-15
      • 1970-01-01
      • 2019-11-24
      • 2019-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多