Skip to content
Browse files

[Form] Protected methods in FormConfigBuilder and FormBuilder from be…

…ing called when it is turned into a FormConfigInterface instance
  • Loading branch information...
1 parent fee1bf5 commit bcc5552de4930c3e5d367c5df9a87b93bf5bc70f @webmozart webmozart committed
View
7 UPGRADE-2.2.md
@@ -57,6 +57,13 @@
* The PasswordType is now not trimmed by default.
+ * The class FormException is now an interface. The old class is still available
+ under the name Symfony\Component\Form\Exception\Exception, but will probably
+ be removed before 2.2. If you created FormException instances manually,
+ you are now advised to create any of the other exceptions in the
+ Symfony\Component\Form\Exception namespace or to create custom exception
+ classes for your purpose.
+
#### Deprecations
* The methods `getParent()`, `setParent()` and `hasParent()` in
View
2 src/Symfony/Component/Form/CHANGELOG.md
@@ -10,6 +10,8 @@ CHANGELOG
* removed special characters between the choice or text fields of DateType unless
the option "format" is set to a custom value
* deprecated FormException and introduced ExceptionInterface instead
+ * [BC BREAK] FormException is now an interface
+ * protected FormBuilder methods from being called when it is turned into a FormConfigInterface with getFormConfig()
2.1.0
-----
View
21 src/Symfony/Component/Form/Exception/BadMethodCallException.php
@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Exception;
+
+/**
+ * Base BadMethodCallException for the Form component.
+ *
+ * @author Bernhard Schussek <bschussek@gmail.com>
+ */
+class BadMethodCallException extends \BadMethodCallException implements ExceptionInterface
+{
+}
View
104 src/Symfony/Component/Form/FormBuilder.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Form;
+use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\Exception;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -65,7 +66,7 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
public function add($child, $type = null, array $options = array())
{
if ($this->locked) {
- throw new Exception('The form builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if ($child instanceof self) {
@@ -102,7 +103,7 @@ public function add($child, $type = null, array $options = array())
public function create($name, $type = null, array $options = array())
{
if ($this->locked) {
- throw new Exception('The form builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (null === $type && null === $this->getDataClass()) {
@@ -121,6 +122,10 @@ public function create($name, $type = null, array $options = array())
*/
public function get($name)
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
if (isset($this->unresolvedChildren[$name])) {
return $this->resolveChild($name);
}
@@ -138,7 +143,7 @@ public function get($name)
public function remove($name)
{
if ($this->locked) {
- throw new Exception('The form builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
unset($this->unresolvedChildren[$name]);
@@ -158,6 +163,10 @@ public function remove($name)
*/
public function has($name)
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
if (isset($this->unresolvedChildren[$name])) {
return true;
}
@@ -174,6 +183,10 @@ public function has($name)
*/
public function all()
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
$this->resolveChildren();
return $this->children;
@@ -184,6 +197,10 @@ public function all()
*/
public function count()
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
return count($this->children);
}
@@ -192,6 +209,10 @@ public function count()
*/
public function getForm()
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
$this->resolveChildren();
$form = new Form($this->getFormConfig());
@@ -208,6 +229,10 @@ public function getForm()
*/
public function getParent()
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
return $this->parent;
}
@@ -217,7 +242,7 @@ public function getParent()
public function setParent(FormBuilderInterface $parent = null)
{
if ($this->locked) {
- throw new Exception('The form builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->parent = $parent;
@@ -230,36 +255,11 @@ public function setParent(FormBuilderInterface $parent = null)
*/
public function hasParent()
{
- return null !== $this->parent;
- }
-
- /**
- * Converts an unresolved child into a {@link FormBuilder} instance.
- *
- * @param string $name The name of the unresolved child.
- *
- * @return FormBuilder The created instance.
- */
- private function resolveChild($name)
- {
- $info = $this->unresolvedChildren[$name];
- $child = $this->create($name, $info['type'], $info['options']);
- $this->children[$name] = $child;
- unset($this->unresolvedChildren[$name]);
-
- return $child;
- }
-
- /**
- * Converts all unresolved children into {@link FormBuilder} instances.
- */
- private function resolveChildren()
- {
- foreach ($this->unresolvedChildren as $name => $info) {
- $this->children[$name] = $this->create($name, $info['type'], $info['options']);
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
- $this->unresolvedChildren = array();
+ return null !== $this->parent;
}
/**
@@ -267,6 +267,10 @@ private function resolveChildren()
*/
public function getIterator()
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
return new \ArrayIterator($this->children);
}
@@ -277,9 +281,16 @@ public function getIterator()
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link FormConfigInterface::getType()} instead.
+ *
+ * @throws BadMethodCallException If the builder was turned into a {@link FormConfigInterface}
+ * via {@link getFormConfig()}.
*/
public function getTypes()
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
trigger_error('getTypes() is deprecated since version 2.1 and will be removed in 2.3. Use getConfig() and FormConfigInterface::getType() instead.', E_USER_DEPRECATED);
$types = array();
@@ -290,4 +301,33 @@ public function getTypes()
return $types;
}
+
+ /**
+ * Converts an unresolved child into a {@link FormBuilder} instance.
+ *
+ * @param string $name The name of the unresolved child.
+ *
+ * @return FormBuilder The created instance.
+ */
+ private function resolveChild($name)
+ {
+ $info = $this->unresolvedChildren[$name];
+ $child = $this->create($name, $info['type'], $info['options']);
+ $this->children[$name] = $child;
+ unset($this->unresolvedChildren[$name]);
+
+ return $child;
+ }
+
+ /**
+ * Converts all unresolved children into {@link FormBuilder} instances.
+ */
+ private function resolveChildren()
+ {
+ foreach ($this->unresolvedChildren as $name => $info) {
+ $this->children[$name] = $this->create($name, $info['type'], $info['options']);
+ }
+
+ $this->unresolvedChildren = array();
+ }
}
View
75 src/Symfony/Component/Form/FormConfigBuilder.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Form;
+use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\Exception;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Util\PropertyPath;
@@ -172,7 +173,7 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
public function addEventListener($eventName, $listener, $priority = 0)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dispatcher->addListener($eventName, $listener, $priority);
@@ -186,7 +187,7 @@ public function addEventListener($eventName, $listener, $priority = 0)
public function addEventSubscriber(EventSubscriberInterface $subscriber)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dispatcher->addSubscriber($subscriber);
@@ -202,7 +203,7 @@ public function addValidator(FormValidatorInterface $validator)
trigger_error('addValidator() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->validators[] = $validator;
@@ -216,7 +217,7 @@ public function addValidator(FormValidatorInterface $validator)
public function addViewTransformer(DataTransformerInterface $viewTransformer, $forcePrepend = false)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if ($forcePrepend) {
@@ -234,7 +235,7 @@ public function addViewTransformer(DataTransformerInterface $viewTransformer, $f
public function resetViewTransformers()
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->viewTransformers = array();
@@ -249,7 +250,7 @@ public function resetViewTransformers()
*
* @return FormConfigBuilder The configuration object.
*
- * @throws Exception if the form configuration is locked
+ * @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link addViewTransformer()} instead.
@@ -259,7 +260,7 @@ public function appendClientTransformer(DataTransformerInterface $viewTransforme
trigger_error('appendClientTransformer() is deprecated since version 2.1 and will be removed in 2.3. Use addViewTransformer() instead.', E_USER_DEPRECATED);
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addViewTransformer($viewTransformer);
@@ -272,7 +273,7 @@ public function appendClientTransformer(DataTransformerInterface $viewTransforme
*
* @return FormConfigBuilder The configuration object.
*
- * @throws Exception if the form configuration is locked
+ * @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
@@ -281,7 +282,7 @@ public function prependClientTransformer(DataTransformerInterface $viewTransform
trigger_error('prependClientTransformer() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addViewTransformer($viewTransformer, true);
@@ -292,7 +293,7 @@ public function prependClientTransformer(DataTransformerInterface $viewTransform
*
* @return FormConfigBuilder The configuration object.
*
- * @throws Exception if the form configuration is locked
+ * @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link resetViewTransformers()} instead.
@@ -302,7 +303,7 @@ public function resetClientTransformers()
trigger_error('resetClientTransformers() is deprecated since version 2.1 and will be removed in 2.3. Use resetViewTransformers() instead.', E_USER_DEPRECATED);
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->resetViewTransformers();
@@ -314,7 +315,7 @@ public function resetClientTransformers()
public function addModelTransformer(DataTransformerInterface $modelTransformer, $forceAppend = false)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if ($forceAppend) {
@@ -332,7 +333,7 @@ public function addModelTransformer(DataTransformerInterface $modelTransformer,
public function resetModelTransformers()
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->modelTransformers = array();
@@ -347,7 +348,7 @@ public function resetModelTransformers()
*
* @return FormConfigBuilder The configuration object.
*
- * @throws Exception if the form configuration is locked
+ * @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3.
*/
@@ -356,7 +357,7 @@ public function appendNormTransformer(DataTransformerInterface $modelTransformer
trigger_error('appendNormTransformer() is deprecated since version 2.1 and will be removed in 2.3.', E_USER_DEPRECATED);
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addModelTransformer($modelTransformer, true);
@@ -369,7 +370,7 @@ public function appendNormTransformer(DataTransformerInterface $modelTransformer
*
* @return FormConfigBuilder The configuration object.
*
- * @throws Exception if the form configuration is locked
+ * @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link addModelTransformer()} instead.
@@ -379,7 +380,7 @@ public function prependNormTransformer(DataTransformerInterface $modelTransforme
trigger_error('prependNormTransformer() is deprecated since version 2.1 and will be removed in 2.3. Use addModelTransformer() instead.', E_USER_DEPRECATED);
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->addModelTransformer($modelTransformer);
@@ -390,7 +391,7 @@ public function prependNormTransformer(DataTransformerInterface $modelTransforme
*
* @return FormConfigBuilder The configuration object.
*
- * @throws Exception if the form configuration is locked
+ * @throws BadMethodCallException if the form configuration is locked
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link resetModelTransformers()} instead.
@@ -400,7 +401,7 @@ public function resetNormTransformers()
trigger_error('resetNormTransformers() is deprecated since version 2.1 and will be removed in 2.3. Use resetModelTransformers() instead.', E_USER_DEPRECATED);
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
return $this->resetModelTransformers();
@@ -652,7 +653,7 @@ public function getOption($name, $default = null)
public function setAttribute($name, $value)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->attributes[$name] = $value;
@@ -666,7 +667,7 @@ public function setAttribute($name, $value)
public function setAttributes(array $attributes)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->attributes = $attributes;
@@ -680,7 +681,7 @@ public function setAttributes(array $attributes)
public function setDataMapper(DataMapperInterface $dataMapper = null)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dataMapper = $dataMapper;
@@ -694,7 +695,7 @@ public function setDataMapper(DataMapperInterface $dataMapper = null)
public function setDisabled($disabled)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->disabled = (Boolean) $disabled;
@@ -708,7 +709,7 @@ public function setDisabled($disabled)
public function setEmptyData($emptyData)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->emptyData = $emptyData;
@@ -722,7 +723,7 @@ public function setEmptyData($emptyData)
public function setErrorBubbling($errorBubbling)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->errorBubbling = null === $errorBubbling ? null : (Boolean) $errorBubbling;
@@ -736,7 +737,7 @@ public function setErrorBubbling($errorBubbling)
public function setRequired($required)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->required = (Boolean) $required;
@@ -750,7 +751,7 @@ public function setRequired($required)
public function setPropertyPath($propertyPath)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
if (null !== $propertyPath && !$propertyPath instanceof PropertyPathInterface) {
@@ -768,7 +769,7 @@ public function setPropertyPath($propertyPath)
public function setMapped($mapped)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->mapped = $mapped;
@@ -782,7 +783,7 @@ public function setMapped($mapped)
public function setByReference($byReference)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->byReference = $byReference;
@@ -796,7 +797,7 @@ public function setByReference($byReference)
public function setVirtual($virtual)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->virtual = $virtual;
@@ -810,7 +811,7 @@ public function setVirtual($virtual)
public function setCompound($compound)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->compound = $compound;
@@ -824,7 +825,7 @@ public function setCompound($compound)
public function setType(ResolvedFormTypeInterface $type)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->type = $type;
@@ -838,7 +839,7 @@ public function setType(ResolvedFormTypeInterface $type)
public function setData($data)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->data = $data;
@@ -852,7 +853,7 @@ public function setData($data)
public function setDataLocked($locked)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->dataLocked = $locked;
@@ -866,7 +867,7 @@ public function setDataLocked($locked)
public function setFormFactory(FormFactoryInterface $formFactory)
{
if ($this->locked) {
- throw new Exception('The config builder cannot be modified anymore.');
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}
$this->formFactory = $formFactory;
@@ -879,6 +880,10 @@ public function setFormFactory(FormFactoryInterface $formFactory)
*/
public function getFormConfig()
{
+ if ($this->locked) {
+ throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
+ }
+
// This method should be idempotent, so clone the builder
$config = clone $this;
$config->locked = true;

0 comments on commit bcc5552

Please sign in to comment.
Something went wrong with that request. Please try again.