0

I'm trying to create a bundle to manage a menu which can be configured via config files.

So I have wrote some configuration constraints in my class DependencyInjection\Configuration.

The needed configuration is an array of items which must be added to the menu. Each item can have 3 different types (link, link_notification, widget). And for each type, item needs other attributes (like route, label, etc.).

Example of configuration:

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 }

I'm stuck because I can't find how to define different array constraints for each type.

I can't find a way to translate condition:

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

Configuration file looks like this:

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;
    }

I have seen methods ->ifXXX() [...] ->then() in Symfony2 documentation, but I can't figure out how to use them in this context.

1 Answer 1

1

It would be something like: (not tested, but it gives you some direction to look for)

$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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.