3

Some of the features of my bundle are optional. In order to configure them, I'd like to have the following two options. If I want to enable the feature:

foo:
  feature:
    mandatory1: 'bar'
    mandatory2: 'bar2'

However, if I decide to not use this specific feature, I'd like to set feature to be null:

foo:
  feature: ~

I currently have the following code:

// ...
    ->arrayNode('feature')
        ->defaultNull()
        ->children()
            ->scalarNode('mandatory1')
                ->isRequired()
                ->cannotBeEmpty()
                ->end()
            ->scalarNode('mandatory2')
                ->isRequired()
                ->cannotBeEmpty()
                ->end()
            ->end()
        ->end()

However, the defaultNull() call is not allowed. Is there any way I can achieve this behaviour?

1 Answer 1

1

Actually, I found the answer by myself. Copied from the Symfony docs:

If you have entire sections which are optional and can be enabled/disabled, you can take advantage of the shortcut canBeEnabled() and canBeDisabled() methods.

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.