From a15e8465682c055d1ee1a779fcbcea3aaa4d3eef Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 18 May 2011 13:14:51 +0200 Subject: [PATCH] added a way to disable forms, and force validation to be enabled when forms are enabled (closes #840) --- UPDATE.md | 24 +++++++++++------ .../DependencyInjection/Configuration.php | 20 +++++++++++--- .../FrameworkExtension.php | 27 +++++++++---------- .../Resources/config/schema/symfony-1.0.xsd | 5 ++++ .../DependencyInjection/Fixtures/php/full.php | 1 + .../DependencyInjection/Fixtures/xml/full.xml | 1 + .../DependencyInjection/Fixtures/yml/full.yml | 1 + 7 files changed, 53 insertions(+), 26 deletions(-) diff --git a/UPDATE.md b/UPDATE.md index 2b75f7335f..7dd475183f 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -9,19 +9,27 @@ timeline closely anyway. beta1 to beta2 -------------- +* Forms must now be explicitly enabled (automatically done in Symfony SE): + + form: ~ + + # equivalent to + form: + enabled: true + * The Routing Exceptions have been moved: - Before: + Before: - Symfony\Component\Routing\Matcher\Exception\Exception - Symfony\Component\Routing\Matcher\Exception\NotFoundException - Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException + Symfony\Component\Routing\Matcher\Exception\Exception + Symfony\Component\Routing\Matcher\Exception\NotFoundException + Symfony\Component\Routing\Matcher\Exception\MethodNotAllowedException - After: + After: - Symfony\Component\Routing\Exception\Exception - Symfony\Component\Routing\Exception\NotFoundException - Symfony\Component\Routing\Exception\MethodNotAllowedException + Symfony\Component\Routing\Exception\Exception + Symfony\Component\Routing\Exception\NotFoundException + Symfony\Component\Routing\Exception\MethodNotAllowedException * The form component's ``csrf_page_id`` option has been renamed to ``intention``. diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index fab0524788..883e43f643 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -46,7 +46,7 @@ class Configuration implements ConfigurationInterface ->end() ; - $this->addCsrfProtectionSection($rootNode); + $this->addFormSection($rootNode); $this->addEsiSection($rootNode); $this->addProfilerSection($rootNode); $this->addRouterSection($rootNode); @@ -58,10 +58,18 @@ class Configuration implements ConfigurationInterface return $treeBuilder; } - private function addCsrfProtectionSection(ArrayNodeDefinition $rootNode) + private function addFormSection(ArrayNodeDefinition $rootNode) { $rootNode ->children() + ->arrayNode('form') + ->canBeUnset() + ->treatNullLike(array('enabled' => true)) + ->treatTrueLike(array('enabled' => true)) + ->children() + ->booleanNode('enabled')->defaultTrue()->end() + ->end() + ->end() ->arrayNode('csrf_protection') ->canBeUnset() ->treatNullLike(array('enabled' => true)) @@ -84,7 +92,7 @@ class Configuration implements ConfigurationInterface ->treatNullLike(array('enabled' => true)) ->treatTrueLike(array('enabled' => true)) ->children() - ->booleanNode('enabled')->end() + ->booleanNode('enabled')->defaultTrue()->end() ->end() ->end() ->end() @@ -228,6 +236,8 @@ class Configuration implements ConfigurationInterface ->children() ->arrayNode('translator') ->canBeUnset() + ->treatNullLike(array('enabled' => true)) + ->treatTrueLike(array('enabled' => true)) ->children() ->booleanNode('enabled')->defaultTrue()->end() ->scalarNode('fallback')->defaultValue('en')->end() @@ -243,6 +253,8 @@ class Configuration implements ConfigurationInterface ->children() ->arrayNode('validation') ->canBeUnset() + ->treatNullLike(array('enabled' => true)) + ->treatTrueLike(array('enabled' => true)) // For XML, namespace is a child of validation, so it must be moved under annotations ->beforeNormalization() ->ifTrue(function($v) { return is_array($v) && !empty($v['annotations']) && !empty($v['namespace']); }) @@ -253,7 +265,7 @@ class Configuration implements ConfigurationInterface }) ->end() ->children() - ->booleanNode('enabled')->end() + ->booleanNode('enabled')->defaultTrue()->end() ->scalarNode('cache')->end() ->arrayNode('annotations') ->canBeUnset() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index fc342a3294..2bf9e7e2d6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -69,7 +69,18 @@ class FrameworkExtension extends Extension $loader->load('test.xml'); } - $this->registerFormConfiguration($config, $container, $loader); + if (isset($config['session'])) { + $this->registerSessionConfiguration($config['session'], $container, $loader); + } + + if ($hasForm = isset($config['form']) && !empty($config['form']['enabled'])) { + $this->registerFormConfiguration($config, $container, $loader); + $config['validation']['enabled'] = true; + } + + if (!empty($config['validation']['enabled'])) { + $this->registerValidationConfiguration($config['validation'], $container, $loader); + } if (isset($config['esi'])) { $this->registerEsiConfiguration($config['esi'], $loader); @@ -83,10 +94,6 @@ class FrameworkExtension extends Extension $this->registerRouterConfiguration($config['router'], $container, $loader); } - if (isset($config['session'])) { - $this->registerSessionConfiguration($config['session'], $container, $loader); - } - if (isset($config['templating'])) { $this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader); } @@ -95,10 +102,6 @@ class FrameworkExtension extends Extension $this->registerTranslatorConfiguration($config['translator'], $container); } - if (isset($config['validation'])) { - $this->registerValidationConfiguration($config['validation'], $container, $loader); - } - $this->addClassesToCompile(array( 'Symfony\\Component\\HttpFoundation\\ParameterBag', 'Symfony\\Component\\HttpFoundation\\HeaderBag', @@ -151,7 +154,7 @@ class FrameworkExtension extends Extension $container->setParameter('form.type_extension.csrf.field_name', $config['csrf_protection']['field_name']); } - if (isset($config['session'])) { + if ($container->hasDefinition('session')) { $container->removeDefinition('file.temporary_storage'); $container->setDefinition('file.temporary_storage', $container->getDefinition('file.temporary_storage.session')); $container->removeDefinition('file.temporary_storage.session'); @@ -453,10 +456,6 @@ class FrameworkExtension extends Extension */ private function registerValidationConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader) { - if (empty($config['enabled'])) { - return; - } - $loader->load('validator.xml'); $container->setParameter('validator.mapping.loader.xml_files_loader.mapping_files', $this->getValidatorXmlMappingFiles($container)); diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 8735a1a6e9..9b3b6b6e9a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -9,6 +9,7 @@ + @@ -35,6 +36,10 @@ + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php index b11be834f4..fe07faa3da 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php @@ -2,6 +2,7 @@ $container->loadFromExtension('framework', array( 'secret' => 's3cr3t', + 'form' => null, 'csrf_protection' => array( 'enabled' => true, 'field_name' => '_csrf', diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml index e4071758f9..98eaf57762 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml @@ -8,6 +8,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml index 028cb7adf7..27aa9d7606 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml @@ -1,5 +1,6 @@ framework: secret: s3cr3t + form: ~ csrf_protection: enabled: true field_name: _csrf