2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2010-10-02 11:38:11 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-06-24 09:40:05 +01:00
|
|
|
*
|
2011-01-15 13:29:43 +00:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2010-06-24 09:40:05 +01:00
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Form;
|
|
|
|
|
2010-12-15 14:36:47 +00:00
|
|
|
use Symfony\Component\Form\Exception\FormException;
|
2012-02-10 12:47:43 +00:00
|
|
|
use Symfony\Component\Form\Exception\AlreadyBoundException;
|
2011-01-29 21:39:36 +00:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
2011-04-22 16:41:21 +01:00
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
2012-05-17 15:09:13 +01:00
|
|
|
use Symfony\Component\Form\Util\PropertyPath;
|
2011-04-22 16:41:21 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2011-03-16 17:20:13 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2010-10-02 11:38:11 +01:00
|
|
|
|
2010-06-24 09:40:05 +01:00
|
|
|
/**
|
|
|
|
* Form represents a form.
|
|
|
|
*
|
|
|
|
* A form is composed of a validator schema and a widget form schema.
|
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* To implement your own form fields, you need to have a thorough understanding
|
|
|
|
* of the data flow within a form field. A form field stores its data in three
|
|
|
|
* different representations:
|
|
|
|
*
|
|
|
|
* (1) the format required by the form's object
|
|
|
|
* (2) a normalized format for internal processing
|
|
|
|
* (3) the format used for display
|
|
|
|
*
|
|
|
|
* A date field, for example, may store a date as "Y-m-d" string (1) in the
|
|
|
|
* object. To facilitate processing in the field, this value is normalized
|
|
|
|
* to a DateTime object (2). In the HTML representation of your form, a
|
|
|
|
* localized string (3) is presented to and modified by the user.
|
|
|
|
*
|
|
|
|
* In most cases, format (1) and format (2) will be the same. For example,
|
|
|
|
* a checkbox field uses a Boolean value both for internal processing as for
|
|
|
|
* storage in the object. In these cases you simply need to set a value
|
|
|
|
* transformer to convert between formats (2) and (3). You can do this by
|
2011-08-14 16:42:02 +01:00
|
|
|
* calling appendClientTransformer().
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
|
|
|
* In some cases though it makes sense to make format (1) configurable. To
|
|
|
|
* demonstrate this, let's extend our above date field to store the value
|
|
|
|
* either as "Y-m-d" string or as timestamp. Internally we still want to
|
|
|
|
* use a DateTime object for processing. To convert the data from string/integer
|
|
|
|
* to DateTime you can set a normalization transformer by calling
|
2011-08-14 16:42:02 +01:00
|
|
|
* appendNormTransformer(). The normalized data is then
|
2011-03-20 12:35:19 +00:00
|
|
|
* converted to the displayed data as described before.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony.com>
|
2010-06-24 09:40:05 +01:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
class Form implements \IteratorAggregate, FormInterface
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2011-01-25 08:56:37 +00:00
|
|
|
/**
|
2012-05-16 18:25:27 +01:00
|
|
|
* The form's configuration
|
|
|
|
* @var FormConfigInterface
|
2011-03-31 14:23:33 +01:00
|
|
|
*/
|
2012-05-16 18:25:27 +01:00
|
|
|
private $config;
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* The parent of this form
|
2011-03-31 14:23:33 +01:00
|
|
|
* @var FormInterface
|
|
|
|
*/
|
|
|
|
private $parent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The children of this form
|
2011-05-10 17:23:58 +01:00
|
|
|
* @var array An array of FormInterface instances
|
2011-01-25 08:56:37 +00:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
private $children = array();
|
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
/**
|
|
|
|
* The errors of this form
|
2012-03-09 08:14:16 +00:00
|
|
|
* @var array An array of FormError instances
|
2011-03-31 14:23:33 +01:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
private $errors = array();
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether this form is bound
|
|
|
|
* @var Boolean
|
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
private $bound = false;
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-23 19:21:34 +01:00
|
|
|
* The form data in model format
|
2011-03-31 14:23:33 +01:00
|
|
|
* @var mixed
|
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
private $modelData;
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The form data in normalized format
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
private $normData;
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-23 19:21:34 +01:00
|
|
|
* The form data in view format
|
2011-03-31 14:23:33 +01:00
|
|
|
* @var mixed
|
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
private $viewData;
|
2011-01-29 21:39:36 +00:00
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* The bound values that don't belong to any children
|
2011-01-29 21:39:36 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2011-03-20 11:00:19 +00:00
|
|
|
private $extraData = array();
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
/**
|
2012-05-23 19:21:34 +01:00
|
|
|
* Whether the data in model, normalized and view format is
|
2011-03-31 14:23:33 +01:00
|
|
|
* synchronized. Data may not be synchronized if transformation errors
|
|
|
|
* occur.
|
|
|
|
* @var Boolean
|
|
|
|
*/
|
2011-03-22 01:03:22 +00:00
|
|
|
private $synchronized = true;
|
2011-03-31 14:23:33 +01:00
|
|
|
|
|
|
|
/**
|
2012-05-16 18:25:27 +01:00
|
|
|
* Creates a new form based on the given configuration.
|
|
|
|
*
|
|
|
|
* @param FormConfigInterface $config The form configuration.
|
2011-03-31 14:23:33 +01:00
|
|
|
*/
|
2012-05-16 18:25:27 +01:00
|
|
|
public function __construct(FormConfigInterface $config)
|
2011-03-01 13:19:28 +00:00
|
|
|
{
|
2012-05-20 10:18:31 +01:00
|
|
|
if (!$config instanceof UnmodifiableFormConfig) {
|
|
|
|
$config = new UnmodifiableFormConfig($config);
|
2011-04-02 15:39:19 +01:00
|
|
|
}
|
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->config = $config;
|
2011-04-02 15:39:19 +01:00
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->setData($config->getData());
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 21:10:53 +00:00
|
|
|
public function __clone()
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-21 21:10:53 +00:00
|
|
|
foreach ($this->children as $key => $child) {
|
|
|
|
$this->children[$key] = clone $child;
|
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
/**
|
|
|
|
* Returns the configuration of the form.
|
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return UnmodifiableFormConfig The form's immutable configuration.
|
2012-05-16 18:25:27 +01:00
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns the name by which the form is identified in forms.
|
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return string The name of the form.
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function getName()
|
2011-03-18 11:50:26 +00:00
|
|
|
{
|
2012-05-16 18:25:27 +01:00
|
|
|
return $this->config->getName();
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2012-05-17 15:09:13 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getPropertyPath()
|
|
|
|
{
|
2012-05-18 10:20:03 +01:00
|
|
|
if (null !== $this->config->getPropertyPath()) {
|
2012-05-17 15:09:13 +01:00
|
|
|
return $this->config->getPropertyPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $this->getName() || '' === $this->getName()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-05-18 10:20:03 +01:00
|
|
|
if ($this->hasParent() && null === $this->getParent()->getConfig()->getDataClass()) {
|
2012-05-17 15:09:13 +01:00
|
|
|
return new PropertyPath('[' . $this->getName() . ']');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new PropertyPath($this->getName());
|
|
|
|
}
|
|
|
|
|
2011-05-10 14:32:14 +01:00
|
|
|
/**
|
2011-05-10 17:23:58 +01:00
|
|
|
* Returns the types used by this form.
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
|
|
|
* @return array An array of FormTypeInterface
|
2012-05-17 15:09:13 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
|
|
|
* {@link getConfig()} and {@link FormConfigInterface::getTypes()} instead.
|
2011-05-10 14:32:14 +01:00
|
|
|
*/
|
2011-03-24 15:16:31 +00:00
|
|
|
public function getTypes()
|
|
|
|
{
|
2012-05-16 18:25:27 +01:00
|
|
|
return $this->config->getTypes();
|
2011-03-24 15:16:31 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2012-05-16 18:25:27 +01:00
|
|
|
* {@inheritdoc}
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function isRequired()
|
|
|
|
{
|
|
|
|
if (null === $this->parent || $this->parent->isRequired()) {
|
2012-05-16 18:25:27 +01:00
|
|
|
return $this->config->getRequired();
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2012-01-26 15:54:42 +00:00
|
|
|
* {@inheritDoc}
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
2012-01-26 15:54:42 +00:00
|
|
|
public function isDisabled()
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-01-26 15:54:42 +00:00
|
|
|
if (null === $this->parent || !$this->parent->isDisabled()) {
|
2012-05-16 18:25:27 +01:00
|
|
|
return $this->config->getDisabled();
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
return true;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Sets the parent form.
|
|
|
|
*
|
|
|
|
* @param FormInterface $parent The parent form
|
|
|
|
*
|
|
|
|
* @return Form The current form
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function setParent(FormInterface $parent = null)
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2012-02-10 12:47:43 +00:00
|
|
|
if ($this->bound) {
|
|
|
|
throw new AlreadyBoundException('You cannot set the parent of a bound form');
|
|
|
|
}
|
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
if ('' === $this->config->getName()) {
|
|
|
|
throw new FormException('A form with an empty name cannot have a parent form.');
|
2012-01-07 14:14:50 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->parent = $parent;
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
return $this;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-13 15:06:32 +01:00
|
|
|
* Returns the parent form.
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2012-04-13 15:06:32 +01:00
|
|
|
* @return FormInterface The parent form
|
2011-01-29 21:39:36 +00:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function getParent()
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
return $this->parent;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns whether the form has a parent.
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return Boolean
|
2011-01-29 21:39:36 +00:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function hasParent()
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
return null !== $this->parent;
|
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns the root of the form tree.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return FormInterface The root of the tree
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function getRoot()
|
|
|
|
{
|
|
|
|
return $this->parent ? $this->parent->getRoot() : $this;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2012-04-13 15:06:32 +01:00
|
|
|
* Returns whether the form is the root of the form tree.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function isRoot()
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
return !$this->hasParent();
|
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
|
2011-05-10 14:32:14 +01:00
|
|
|
/**
|
|
|
|
* Returns whether the form has an attribute with the given name.
|
|
|
|
*
|
2012-05-17 15:09:13 +01:00
|
|
|
* @param string $name The name of the attribute.
|
2011-08-14 16:42:02 +01:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return Boolean Whether the attribute exists.
|
2012-05-23 17:47:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
|
|
|
* {@link getConfig()} and {@link FormConfigInterface::hasAttribute()} instead.
|
2011-05-10 14:32:14 +01:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function hasAttribute($name)
|
|
|
|
{
|
2012-05-16 18:25:27 +01:00
|
|
|
return $this->config->hasAttribute($name);
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 14:32:14 +01:00
|
|
|
/**
|
|
|
|
* Returns the value of the attributes with the given name.
|
|
|
|
*
|
2012-05-17 15:09:13 +01:00
|
|
|
* @param string $name The name of the attribute
|
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return mixed The attribute value.
|
2012-05-23 17:47:14 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
|
|
|
* {@link getConfig()} and {@link FormConfigInterface::getAttribute()} instead.
|
2011-05-10 14:32:14 +01:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function getAttribute($name)
|
2011-03-18 11:50:26 +00:00
|
|
|
{
|
2012-05-16 18:25:27 +01:00
|
|
|
return $this->config->getAttribute($name);
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2012-04-13 15:06:32 +01:00
|
|
|
* Updates the form with default data.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2012-05-23 19:21:34 +01:00
|
|
|
* @param array $modelData The data formatted as expected for the underlying object
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
|
|
|
* @return Form The current form
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
public function setData($modelData)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-02-10 12:47:43 +00:00
|
|
|
if ($this->bound) {
|
|
|
|
throw new AlreadyBoundException('You cannot change the data of a bound form');
|
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
if (is_object($modelData) && !$this->config->getByReference()) {
|
|
|
|
$modelData = clone $modelData;
|
2012-05-23 13:51:26 +01:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
// Hook to change content of the data
|
2012-05-23 19:58:42 +01:00
|
|
|
$event = new FormEvent($this, $modelData);
|
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::PRE_SET_DATA, $event);
|
|
|
|
// BC until 2.3
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::SET_DATA, $event);
|
2012-05-23 19:21:34 +01:00
|
|
|
$modelData = $event->getData();
|
2011-03-20 12:35:19 +00:00
|
|
|
|
2011-03-27 22:09:46 +01:00
|
|
|
// Treat data as strings unless a value transformer exists
|
2012-05-23 19:21:34 +01:00
|
|
|
if (!$this->config->getViewTransformers() && !$this->config->getModelTransformers() && is_scalar($modelData)) {
|
|
|
|
$modelData = (string) $modelData;
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
// Synchronize representations - must not change the content!
|
2012-05-23 19:21:34 +01:00
|
|
|
$normData = $this->modelToNorm($modelData);
|
|
|
|
$viewData = $this->normToView($normData);
|
2011-03-20 12:35:19 +00:00
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
// Validate if view data matches data class (unless empty)
|
|
|
|
if (!empty($viewData)) {
|
2012-05-17 15:09:13 +01:00
|
|
|
$dataClass = $this->config->getDataClass();
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
if (null === $dataClass && is_object($viewData) && !$viewData instanceof \ArrayAccess) {
|
2012-05-22 17:24:59 +01:00
|
|
|
$expectedType = 'scalar, array or an instance of \ArrayAccess';
|
2012-05-20 10:18:31 +01:00
|
|
|
|
|
|
|
throw new FormException(
|
2012-05-23 19:21:34 +01:00
|
|
|
'The form\'s view data is expected to be of type ' . $expectedType . ', ' .
|
|
|
|
'but is an instance of class ' . get_class($viewData) . '. You ' .
|
2012-05-20 10:18:31 +01:00
|
|
|
'can avoid this error by setting the "data_class" option to ' .
|
2012-05-23 19:21:34 +01:00
|
|
|
'"' . get_class($viewData) . '" or by adding a view transformer ' .
|
|
|
|
'that transforms ' . get_class($viewData) . ' to ' . $expectedType . '.'
|
2012-05-20 10:18:31 +01:00
|
|
|
);
|
2012-05-17 15:09:13 +01:00
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
if (null !== $dataClass && !$viewData instanceof $dataClass) {
|
2012-05-20 10:18:31 +01:00
|
|
|
throw new FormException(
|
2012-05-23 19:21:34 +01:00
|
|
|
'The form\'s view data is expected to be an instance of class ' .
|
|
|
|
$dataClass . ', but has the type ' . gettype($viewData) . '. You ' .
|
2012-05-20 10:18:31 +01:00
|
|
|
'can avoid this error by setting the "data_class" option to ' .
|
2012-05-23 19:21:34 +01:00
|
|
|
'null or by adding a view transformer that transforms ' .
|
|
|
|
gettype($viewData) . ' to ' . $dataClass . '.'
|
2012-05-20 10:18:31 +01:00
|
|
|
);
|
2012-05-17 15:09:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
$this->modelData = $modelData;
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->normData = $normData;
|
2012-05-23 19:21:34 +01:00
|
|
|
$this->viewData = $viewData;
|
2011-03-22 01:03:22 +00:00
|
|
|
$this->synchronized = true;
|
2011-03-20 12:35:19 +00:00
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
if (count($this->children) > 0 && $this->config->getDataMapper()) {
|
2011-03-20 12:35:19 +00:00
|
|
|
// Update child forms from the data
|
2012-05-23 19:21:34 +01:00
|
|
|
$this->config->getDataMapper()->mapDataToForms($viewData, $this->children);
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 19:58:42 +01:00
|
|
|
$event = new FormEvent($this, $modelData);
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::POST_SET_DATA, $event);
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
return $this;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns the data in the format needed for the underlying object.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2011-05-10 14:32:14 +01:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getData()
|
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
return $this->modelData;
|
2011-05-10 14:32:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the data transformed by the value transformer.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
public function getViewData()
|
|
|
|
{
|
|
|
|
return $this->viewData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias of {@link getViewData()}.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
|
|
|
* {@link getViewData()} instead.
|
|
|
|
*/
|
2011-05-10 14:32:14 +01:00
|
|
|
public function getClientData()
|
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
return $this->getViewData();
|
2011-05-10 14:32:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the extra data.
|
|
|
|
*
|
|
|
|
* @return array The bound data which do not belong to a child
|
|
|
|
*/
|
|
|
|
public function getExtraData()
|
|
|
|
{
|
|
|
|
return $this->extraData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-13 15:06:32 +01:00
|
|
|
* Binds data to the form, transforms and validates it.
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
2012-05-23 19:21:34 +01:00
|
|
|
* @param string|array $submittedData The data
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
|
|
|
* @return Form The current form
|
|
|
|
*
|
|
|
|
* @throws UnexpectedTypeException
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
public function bind($submittedData)
|
2011-03-01 13:19:28 +00:00
|
|
|
{
|
2012-02-10 12:47:43 +00:00
|
|
|
if ($this->bound) {
|
|
|
|
throw new AlreadyBoundException('A form can only be bound once');
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
if ($this->isDisabled()) {
|
2011-05-19 00:24:28 +01:00
|
|
|
$this->bound = true;
|
|
|
|
|
2011-05-11 16:08:53 +01:00
|
|
|
return $this;
|
2011-04-02 10:39:15 +01:00
|
|
|
}
|
|
|
|
|
2012-04-10 10:51:28 +01:00
|
|
|
// Don't convert NULL to a string here in order to determine later
|
|
|
|
// whether an empty value has been submitted or whether no value has
|
|
|
|
// been submitted at all. This is important for processing checkboxes
|
|
|
|
// and radio buttons with empty values.
|
2012-05-23 19:21:34 +01:00
|
|
|
if (is_scalar($submittedData)) {
|
|
|
|
$submittedData = (string) $submittedData;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
2011-03-16 17:20:13 +00:00
|
|
|
|
2011-03-24 23:09:08 +00:00
|
|
|
// Initialize errors in the very beginning so that we don't lose any
|
|
|
|
// errors added during listeners
|
|
|
|
$this->errors = array();
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
$modelData = null;
|
2011-03-20 12:35:19 +00:00
|
|
|
$normData = null;
|
|
|
|
$extraData = array();
|
2011-03-22 01:03:22 +00:00
|
|
|
$synchronized = false;
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
// Hook to change content of the data bound by the browser
|
2012-05-23 19:58:42 +01:00
|
|
|
$event = new FormEvent($this, $submittedData);
|
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::PRE_BIND, $event);
|
|
|
|
// BC until 2.3
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::BIND_CLIENT_DATA, $event);
|
2012-05-23 19:21:34 +01:00
|
|
|
$submittedData = $event->getData();
|
|
|
|
|
|
|
|
// Build the data in the view format
|
|
|
|
$viewData = $submittedData;
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
if (count($this->children) > 0) {
|
2012-05-23 19:21:34 +01:00
|
|
|
if (null === $viewData || '' === $viewData) {
|
|
|
|
$viewData = array();
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
if (!is_array($viewData)) {
|
|
|
|
throw new UnexpectedTypeException($viewData, 'array');
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->children as $name => $child) {
|
2012-05-23 19:21:34 +01:00
|
|
|
if (!isset($viewData[$name])) {
|
|
|
|
$viewData[$name] = null;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
foreach ($viewData as $name => $value) {
|
2011-03-20 12:35:19 +00:00
|
|
|
if ($this->has($name)) {
|
|
|
|
$this->children[$name]->bind($value);
|
|
|
|
} else {
|
|
|
|
$extraData[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
// If we have a data mapper, use old view data and merge
|
2011-04-02 11:00:19 +01:00
|
|
|
// data from the children into it later
|
2012-05-16 18:25:27 +01:00
|
|
|
if ($this->config->getDataMapper()) {
|
2012-05-23 19:21:34 +01:00
|
|
|
$viewData = $this->getViewData();
|
2011-04-02 11:00:19 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-20 12:35:19 +00:00
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
if (null === $viewData || '' === $viewData) {
|
2012-05-16 18:25:27 +01:00
|
|
|
$emptyData = $this->config->getEmptyData();
|
2011-03-27 22:07:30 +01:00
|
|
|
|
2012-04-10 10:51:28 +01:00
|
|
|
if ($emptyData instanceof \Closure) {
|
2012-05-23 19:21:34 +01:00
|
|
|
/* @var \Closure $emptyData */
|
|
|
|
$emptyData = $emptyData($this, $viewData);
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
2012-04-10 10:51:28 +01:00
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
$viewData = $emptyData;
|
2011-03-19 14:06:54 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
// Merge form data from children into existing view data
|
|
|
|
if (count($this->children) > 0 && $this->config->getDataMapper() && null !== $viewData) {
|
|
|
|
$this->config->getDataMapper()->mapFormsToData($this->children, $viewData);
|
2011-04-02 11:00:19 +01:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
try {
|
|
|
|
// Normalize data to unified representation
|
2012-05-23 19:21:34 +01:00
|
|
|
$normData = $this->viewToNorm($viewData);
|
2011-03-22 01:03:22 +00:00
|
|
|
$synchronized = true;
|
2011-03-20 12:35:19 +00:00
|
|
|
} catch (TransformationFailedException $e) {
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 01:03:22 +00:00
|
|
|
if ($synchronized) {
|
2012-03-09 08:14:16 +00:00
|
|
|
// Hook to change content of the data into the normalized
|
2011-03-20 12:35:19 +00:00
|
|
|
// representation
|
2012-05-23 19:58:42 +01:00
|
|
|
$event = new FormEvent($this, $normData);
|
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::BIND, $event);
|
|
|
|
// BC until 2.3
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::BIND_NORM_DATA, $event);
|
2011-03-20 12:35:19 +00:00
|
|
|
$normData = $event->getData();
|
|
|
|
|
2012-05-23 19:58:42 +01:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
// Synchronize representations - must not change the content!
|
2012-05-23 19:21:34 +01:00
|
|
|
$modelData = $this->normToModel($normData);
|
|
|
|
$viewData = $this->normToView($normData);
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->bound = true;
|
2012-05-23 19:21:34 +01:00
|
|
|
$this->modelData = $modelData;
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->normData = $normData;
|
2012-05-23 19:21:34 +01:00
|
|
|
$this->viewData = $viewData;
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->extraData = $extraData;
|
2011-03-22 01:03:22 +00:00
|
|
|
$this->synchronized = $synchronized;
|
2011-03-19 18:53:39 +00:00
|
|
|
|
2012-05-23 19:58:42 +01:00
|
|
|
$event = new FormEvent($this, $viewData);
|
2012-05-16 18:25:27 +01:00
|
|
|
$this->config->getEventDispatcher()->dispatch(FormEvents::POST_BIND, $event);
|
2011-03-20 12:35:19 +00:00
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
foreach ($this->config->getValidators() as $validator) {
|
2011-03-20 12:35:19 +00:00
|
|
|
$validator->validate($this);
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
2012-03-15 16:41:21 +00:00
|
|
|
|
2011-05-10 13:40:20 +01:00
|
|
|
return $this;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
2011-03-01 13:19:28 +00:00
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Binds a request to the form.
|
2011-03-31 14:23:33 +01:00
|
|
|
*
|
2011-05-10 14:32:14 +01:00
|
|
|
* If the request method is POST, PUT or GET, the data is bound to the form,
|
2011-03-31 14:23:33 +01:00
|
|
|
* transformed and written into the form data (an object or an array).
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param Request $request The request to bind to the form
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
2011-05-10 13:40:20 +01:00
|
|
|
* @return Form This form
|
|
|
|
*
|
2011-05-10 14:32:14 +01:00
|
|
|
* @throws FormException if the method of the request is not one of GET, POST or PUT
|
2011-03-31 14:23:33 +01:00
|
|
|
*/
|
|
|
|
public function bindRequest(Request $request)
|
|
|
|
{
|
2012-05-16 18:25:27 +01:00
|
|
|
$name = $this->config->getName();
|
|
|
|
|
2011-03-31 14:23:33 +01:00
|
|
|
// Store the bound data in case of a post request
|
|
|
|
switch ($request->getMethod()) {
|
|
|
|
case 'POST':
|
|
|
|
case 'PUT':
|
2012-01-20 00:04:31 +00:00
|
|
|
case 'DELETE':
|
2012-01-24 18:46:37 +00:00
|
|
|
case 'PATCH':
|
2012-05-16 18:25:27 +01:00
|
|
|
if ('' === $name) {
|
2012-02-10 09:36:16 +00:00
|
|
|
// Form bound without name
|
|
|
|
$params = $request->request->all();
|
|
|
|
$files = $request->files->all();
|
|
|
|
} elseif ($this->hasChildren()) {
|
|
|
|
// Form bound with name and children
|
2012-05-16 18:25:27 +01:00
|
|
|
$params = $request->request->get($name, array());
|
|
|
|
$files = $request->files->get($name, array());
|
2011-12-21 14:50:59 +00:00
|
|
|
} else {
|
2012-02-10 09:36:16 +00:00
|
|
|
// Form bound with name, but without children
|
2012-05-16 18:25:27 +01:00
|
|
|
$params = $request->request->get($name, null);
|
|
|
|
$files = $request->files->get($name, null);
|
2012-02-10 09:36:16 +00:00
|
|
|
}
|
|
|
|
if (is_array($params) && is_array($files)) {
|
|
|
|
$data = array_replace_recursive($params, $files);
|
|
|
|
} else {
|
|
|
|
$data = $params ?: $files;
|
2011-12-21 14:50:59 +00:00
|
|
|
}
|
2011-03-31 14:23:33 +01:00
|
|
|
break;
|
|
|
|
case 'GET':
|
2012-05-16 18:25:27 +01:00
|
|
|
$data = '' === $name ? $request->query->all() : $request->query->get($name, array());
|
2011-03-31 14:23:33 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new FormException(sprintf('The request method "%s" is not supported', $request->getMethod()));
|
|
|
|
}
|
|
|
|
|
2011-05-10 13:40:20 +01:00
|
|
|
return $this->bind($data);
|
2011-03-31 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2012-04-13 15:06:32 +01:00
|
|
|
* Returns the normalized data of the form.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return mixed When the form is not bound, the default data is returned.
|
2012-04-13 15:06:32 +01:00
|
|
|
* When the form is bound, the normalized bound data is
|
|
|
|
* returned if the form is valid, null otherwise.
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function getNormData()
|
|
|
|
{
|
|
|
|
return $this->normData;
|
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 13:40:20 +01:00
|
|
|
* Adds an error to this form.
|
|
|
|
*
|
|
|
|
* @param FormError $error
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2011-05-10 13:40:20 +01:00
|
|
|
* @return Form The current form
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
2011-03-21 20:01:14 +00:00
|
|
|
public function addError(FormError $error)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-04-13 15:06:32 +01:00
|
|
|
if ($this->parent && $this->getErrorBubbling()) {
|
2011-03-21 21:10:53 +00:00
|
|
|
$this->parent->addError($error);
|
|
|
|
} else {
|
|
|
|
$this->errors[] = $error;
|
|
|
|
}
|
2011-05-10 13:40:20 +01:00
|
|
|
|
|
|
|
return $this;
|
2011-03-21 21:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns whether errors bubble up to the parent.
|
2011-03-21 21:10:53 +00:00
|
|
|
*
|
|
|
|
* @return Boolean
|
2012-05-17 15:09:13 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
|
|
|
* {@link getConfig()} and {@link FormConfigInterface::getErrorBubbling()} instead.
|
2011-03-21 21:10:53 +00:00
|
|
|
*/
|
|
|
|
public function getErrorBubbling()
|
|
|
|
{
|
2012-05-16 18:25:27 +01:00
|
|
|
return $this->config->getErrorBubbling();
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-13 15:06:32 +01:00
|
|
|
* Returns whether the form is bound.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2011-05-10 14:32:14 +01:00
|
|
|
* @return Boolean true if the form is bound to input values, false otherwise
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function isBound()
|
|
|
|
{
|
|
|
|
return $this->bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns whether the data in the different formats is synchronized.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
2011-03-22 01:03:22 +00:00
|
|
|
public function isSynchronized()
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2011-03-22 01:03:22 +00:00
|
|
|
return $this->synchronized;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns whether the form is empty.
|
|
|
|
*
|
|
|
|
* @return Boolean
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
foreach ($this->children as $child) {
|
|
|
|
if (!$child->isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
return array() === $this->modelData || null === $this->modelData || '' === $this->modelData;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2011-01-29 21:39:36 +00:00
|
|
|
/**
|
2012-04-13 15:06:32 +01:00
|
|
|
* Returns whether the form is valid.
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function isValid()
|
|
|
|
{
|
2012-02-10 12:47:43 +00:00
|
|
|
if (!$this->bound) {
|
2011-07-04 11:12:42 +01:00
|
|
|
throw new \LogicException('You cannot call isValid() on a form that is not bound.');
|
2011-06-28 20:18:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasErrors()) {
|
2011-01-29 21:39:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:54:42 +00:00
|
|
|
if (!$this->isDisabled()) {
|
2011-06-10 15:11:50 +01:00
|
|
|
foreach ($this->children as $child) {
|
|
|
|
if (!$child->isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-20 12:35:19 +00:00
|
|
|
* Returns whether or not there are errors.
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return Boolean true if form is bound and not valid
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function hasErrors()
|
|
|
|
{
|
|
|
|
// Don't call isValid() here, as its semantics are slightly different
|
2012-04-13 15:06:32 +01:00
|
|
|
// Forms are not valid if their children are invalid, but
|
|
|
|
// hasErrors() returns only true if a form itself has errors
|
2011-03-20 12:35:19 +00:00
|
|
|
return count($this->errors) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns all errors.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return array An array of FormError instances that occurred during binding
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function getErrors()
|
|
|
|
{
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
2011-09-25 13:31:31 +01:00
|
|
|
/**
|
|
|
|
* Returns a string representation of all form errors (including children errors).
|
|
|
|
*
|
|
|
|
* This method should only be used to help debug a form.
|
|
|
|
*
|
|
|
|
* @param integer $level The indentation level (used internally)
|
|
|
|
*
|
|
|
|
* @return string A string representation of all errors
|
|
|
|
*/
|
|
|
|
public function getErrorsAsString($level = 0)
|
|
|
|
{
|
|
|
|
$errors = '';
|
|
|
|
foreach ($this->errors as $error) {
|
2011-11-29 17:26:32 +00:00
|
|
|
$errors .= str_repeat(' ', $level).'ERROR: '.$error->getMessage()."\n";
|
2011-09-25 13:31:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->hasChildren()) {
|
|
|
|
foreach ($this->children as $key => $child) {
|
|
|
|
$errors .= str_repeat(' ', $level).$key.":\n";
|
|
|
|
if ($err = $child->getErrorsAsString($level + 4)) {
|
|
|
|
$errors .= $err;
|
|
|
|
} else {
|
|
|
|
$errors .= str_repeat(' ', $level + 4)."No errors\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns the DataTransformers.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2011-05-10 14:32:14 +01:00
|
|
|
* @return array An array of DataTransformerInterface
|
2012-05-17 15:09:13 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
2012-05-23 19:21:34 +01:00
|
|
|
* {@link getConfig()} and {@link FormConfigInterface::getModelTransformers()} instead.
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
2011-04-02 15:39:19 +01:00
|
|
|
public function getNormTransformers()
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
return $this->config->getModelTransformers();
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns the DataTransformers.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2011-05-10 14:32:14 +01:00
|
|
|
* @return array An array of DataTransformerInterface
|
2012-05-17 15:09:13 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
2012-05-23 19:21:34 +01:00
|
|
|
* {@link getConfig()} and {@link FormConfigInterface::getViewTransformers()} instead.
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
2011-04-02 15:39:19 +01:00
|
|
|
public function getClientTransformers()
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
return $this->config->getViewTransformers();
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2012-05-24 13:24:28 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return $this->children;
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Returns all children in this group.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
|
|
|
* @return array
|
2012-05-24 13:24:28 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
|
|
|
* {@link all()} instead.
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function getChildren()
|
|
|
|
{
|
2012-05-24 13:24:28 +01:00
|
|
|
return $this->all();
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 14:32:14 +01:00
|
|
|
/**
|
2012-03-09 08:14:16 +00:00
|
|
|
* Returns whether the form has children.
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
|
|
|
* @return Boolean
|
2012-05-24 13:24:28 +01:00
|
|
|
*
|
|
|
|
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
|
|
|
|
* {@link count()} instead.
|
2011-05-10 14:32:14 +01:00
|
|
|
*/
|
2011-03-22 00:15:48 +00:00
|
|
|
public function hasChildren()
|
|
|
|
{
|
|
|
|
return count($this->children) > 0;
|
|
|
|
}
|
|
|
|
|
2011-05-10 14:32:14 +01:00
|
|
|
/**
|
2012-05-16 18:25:27 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 14:32:14 +01:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function add(FormInterface $child)
|
|
|
|
{
|
2012-02-10 12:47:43 +00:00
|
|
|
if ($this->bound) {
|
|
|
|
throw new AlreadyBoundException('You cannot add children to a bound form');
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->children[$child->getName()] = $child;
|
|
|
|
|
|
|
|
$child->setParent($this);
|
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
if ($this->config->getDataMapper()) {
|
2012-05-23 19:21:34 +01:00
|
|
|
$this->config->getDataMapper()->mapDataToForms($this->getViewData(), array($child));
|
2011-03-22 00:15:48 +00:00
|
|
|
}
|
2011-05-10 13:40:20 +01:00
|
|
|
|
|
|
|
return $this;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 14:32:14 +01:00
|
|
|
/**
|
2012-05-16 18:25:27 +01:00
|
|
|
* {@inheritdoc}
|
2011-05-10 14:32:14 +01:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function remove($name)
|
|
|
|
{
|
2012-02-10 12:47:43 +00:00
|
|
|
if ($this->bound) {
|
|
|
|
throw new AlreadyBoundException('You cannot remove children from a bound form');
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
if (isset($this->children[$name])) {
|
|
|
|
$this->children[$name]->setParent(null);
|
|
|
|
|
|
|
|
unset($this->children[$name]);
|
|
|
|
}
|
2011-05-10 13:40:20 +01:00
|
|
|
|
|
|
|
return $this;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-16 18:25:27 +01:00
|
|
|
* {@inheritdoc}
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
|
|
|
return isset($this->children[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-05-16 18:25:27 +01:00
|
|
|
* {@inheritdoc}
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function get($name)
|
|
|
|
{
|
|
|
|
if (isset($this->children[$name])) {
|
|
|
|
return $this->children[$name];
|
|
|
|
}
|
|
|
|
|
2012-04-13 15:06:32 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('Child "%s" does not exist.', $name));
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the child exists (implements the \ArrayAccess interface).
|
|
|
|
*
|
|
|
|
* @param string $name The name of the child
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
|
|
|
* @return Boolean true if the widget exists, false otherwise
|
|
|
|
*/
|
2011-03-17 10:03:10 +00:00
|
|
|
public function offsetExists($name)
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-17 10:03:10 +00:00
|
|
|
return $this->has($name);
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-20 12:35:19 +00:00
|
|
|
* Returns the form child associated with the name (implements the \ArrayAccess interface).
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2011-03-17 10:03:10 +00:00
|
|
|
* @param string $name The offset of the value to get
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return FormInterface A form instance
|
2011-01-29 21:39:36 +00:00
|
|
|
*/
|
2011-03-17 10:03:10 +00:00
|
|
|
public function offsetGet($name)
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-17 10:03:10 +00:00
|
|
|
return $this->get($name);
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-31 14:23:33 +01:00
|
|
|
* Adds a child to the form (implements the \ArrayAccess interface).
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $name Ignored. The name of the child is used.
|
|
|
|
* @param FormInterface $child The child to be added
|
2011-01-29 21:39:36 +00:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function offsetSet($name, $child)
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-31 14:23:33 +01:00
|
|
|
$this->add($child);
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-31 14:23:33 +01:00
|
|
|
* Removes the child with the given name from the form (implements the \ArrayAccess interface).
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $name The name of the child to be removed
|
2011-01-29 21:39:36 +00:00
|
|
|
*/
|
2011-03-17 10:03:10 +00:00
|
|
|
public function offsetUnset($name)
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-31 14:23:33 +01:00
|
|
|
$this->remove($name);
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the iterator for this group.
|
|
|
|
*
|
|
|
|
* @return \ArrayIterator
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
return new \ArrayIterator($this->children);
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-20 12:35:19 +00:00
|
|
|
* Returns the number of form children (implements the \Countable interface).
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return integer The number of embedded form children
|
2011-01-29 21:39:36 +00:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
return count($this->children);
|
|
|
|
}
|
|
|
|
|
2011-05-06 11:48:43 +01:00
|
|
|
/**
|
|
|
|
* Creates a view.
|
|
|
|
*
|
|
|
|
* @param FormView $parent The parent view
|
|
|
|
*
|
|
|
|
* @return FormView The view
|
|
|
|
*/
|
|
|
|
public function createView(FormView $parent = null)
|
|
|
|
{
|
|
|
|
if (null === $parent && $this->parent) {
|
|
|
|
$parent = $this->parent->createView();
|
|
|
|
}
|
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
$view = new FormView($this->config->getName());
|
2011-05-06 11:48:43 +01:00
|
|
|
|
|
|
|
$view->setParent($parent);
|
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
$types = (array) $this->config->getTypes();
|
2012-05-23 17:47:14 +01:00
|
|
|
$options = $this->config->getOptions();
|
2011-05-06 11:48:43 +01:00
|
|
|
|
|
|
|
foreach ($types as $type) {
|
2012-05-23 17:47:14 +01:00
|
|
|
$type->buildView($view, $this, $options);
|
2011-05-06 11:48:43 +01:00
|
|
|
|
|
|
|
foreach ($type->getExtensions() as $typeExtension) {
|
2012-05-23 17:47:14 +01:00
|
|
|
$typeExtension->buildView($view, $this, $options);
|
2011-05-06 11:48:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 13:11:09 +01:00
|
|
|
foreach ($this->children as $child) {
|
2012-05-24 13:24:28 +01:00
|
|
|
$view->add($child->createView($view));
|
2011-06-15 08:45:11 +01:00
|
|
|
}
|
2011-05-06 11:48:43 +01:00
|
|
|
|
|
|
|
foreach ($types as $type) {
|
2012-05-23 18:34:41 +01:00
|
|
|
$type->finishView($view, $this, $options);
|
2011-05-06 11:48:43 +01:00
|
|
|
|
|
|
|
foreach ($type->getExtensions() as $typeExtension) {
|
2012-05-23 18:34:41 +01:00
|
|
|
$typeExtension->finishView($view, $this, $options);
|
2011-05-06 11:48:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $view;
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
2011-05-10 14:32:14 +01:00
|
|
|
* Normalizes the value if a normalization transformer is set.
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param mixed $value The value to transform
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
private function modelToNorm($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
foreach ($this->config->getModelTransformers() as $transformer) {
|
2011-04-02 15:39:19 +01:00
|
|
|
$value = $transformer->transform($value);
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
return $value;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse transforms a value if a normalization transformer is set.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $value The value to reverse transform
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
private function normToModel($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
$transformers = $this->config->getModelTransformers();
|
2012-05-16 18:25:27 +01:00
|
|
|
|
|
|
|
for ($i = count($transformers) - 1; $i >= 0; --$i) {
|
|
|
|
$value = $transformers[$i]->reverseTransform($value);
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
return $value;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms the value if a value transformer is set.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param mixed $value The value to transform
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
private function normToView($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
if (!$this->config->getViewTransformers()) {
|
2011-03-20 12:35:19 +00:00
|
|
|
// Scalar values should always be converted to strings to
|
|
|
|
// facilitate differentiation between empty ("") and zero (0).
|
2011-05-10 14:32:14 +01:00
|
|
|
return null === $value || is_scalar($value) ? (string) $value : $value;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 19:21:34 +01:00
|
|
|
foreach ($this->config->getViewTransformers() as $transformer) {
|
2011-04-02 15:39:19 +01:00
|
|
|
$value = $transformer->transform($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse transforms a value if a value transformer is set.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param string $value The value to reverse transform
|
2011-05-10 14:32:14 +01:00
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2012-05-23 19:21:34 +01:00
|
|
|
private function viewToNorm($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2012-05-23 19:21:34 +01:00
|
|
|
$transformers = $this->config->getViewTransformers();
|
2012-05-16 18:25:27 +01:00
|
|
|
|
|
|
|
if (!$transformers) {
|
2011-03-20 12:35:19 +00:00
|
|
|
return '' === $value ? null : $value;
|
|
|
|
}
|
|
|
|
|
2012-05-16 18:25:27 +01:00
|
|
|
for ($i = count($transformers) - 1; $i >= 0; --$i) {
|
|
|
|
$value = $transformers[$i]->reverseTransform($value);
|
2011-04-02 15:39:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|