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;
|
|
|
|
|
2011-01-28 22:09:22 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\FileBag;
|
2011-02-03 09:53:51 +00:00
|
|
|
use Symfony\Component\Validator\ExecutionContext;
|
2011-03-17 14:22:10 +00:00
|
|
|
use Symfony\Component\Form\Event\DataEvent;
|
2011-03-16 17:20:13 +00:00
|
|
|
use Symfony\Component\Form\Event\FilterDataEvent;
|
2010-12-15 14:36:47 +00:00
|
|
|
use Symfony\Component\Form\Exception\FormException;
|
2011-02-02 08:41:57 +00:00
|
|
|
use Symfony\Component\Form\Exception\MissingOptionsException;
|
2011-03-17 13:55:11 +00:00
|
|
|
use Symfony\Component\Form\Exception\AlreadyBoundException;
|
2011-01-29 21:39:36 +00:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
|
|
use Symfony\Component\Form\Exception\DanglingFieldException;
|
|
|
|
use Symfony\Component\Form\Exception\FieldDefinitionException;
|
2011-01-25 08:56:37 +00:00
|
|
|
use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
|
2011-03-18 15:01:06 +00:00
|
|
|
use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
|
2011-03-20 12:35:19 +00:00
|
|
|
use Symfony\Component\Form\DataTransformer\TransformationFailedException;
|
2011-03-18 11:50:26 +00:00
|
|
|
use Symfony\Component\Form\DataMapper\DataMapperInterface;
|
2011-03-20 12:35:19 +00:00
|
|
|
use Symfony\Component\Form\Validator\FormValidatorInterface;
|
2011-03-21 19:56:33 +00:00
|
|
|
use Symfony\Component\Form\Renderer\FormRendererInterface;
|
2011-03-16 17:20:13 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
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-01-28 22:09:22 +00:00
|
|
|
* Form also takes care of CSRF protection by default.
|
2010-06-24 09:40:05 +01:00
|
|
|
*
|
2011-01-28 22:09:22 +00:00
|
|
|
* A CSRF secret can be any random string. If set to false, it disables the
|
|
|
|
* CSRF protection, and if set to null, it forces the form to use the global
|
|
|
|
* CSRF secret. If the global CSRF secret is also null, then a random one
|
2010-06-24 09:40:05 +01:00
|
|
|
* is generated on the fly.
|
|
|
|
*
|
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
|
|
|
|
* calling setClientTransformer() in the configure() method.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* setNormTransformer() in configure(). The normalized data is then
|
|
|
|
* 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
|
|
|
/**
|
2011-03-20 12:35:19 +00:00
|
|
|
* Contains all the children of this group
|
2011-01-29 21:39:36 +00:00
|
|
|
* @var array
|
2011-01-25 08:56:37 +00:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
private $children = array();
|
|
|
|
|
|
|
|
private $dataMapper;
|
|
|
|
private $errors = array();
|
|
|
|
private $name = '';
|
|
|
|
private $parent;
|
|
|
|
private $bound = false;
|
|
|
|
private $required;
|
|
|
|
private $data;
|
|
|
|
private $normData;
|
|
|
|
private $clientData;
|
2011-01-29 21:39:36 +00:00
|
|
|
|
|
|
|
/**
|
2011-03-20 12:35:19 +00:00
|
|
|
* Contains the names of bound values who 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-20 12:35:19 +00:00
|
|
|
private $normTransformer;
|
|
|
|
private $clientTransformer;
|
|
|
|
private $transformationSuccessful = true;
|
|
|
|
private $validators;
|
|
|
|
private $renderer;
|
|
|
|
private $readOnly = false;
|
|
|
|
private $dispatcher;
|
|
|
|
private $attributes;
|
2010-06-24 10:24:08 +01:00
|
|
|
|
2011-03-17 10:03:10 +00:00
|
|
|
public function __construct($name, EventDispatcherInterface $dispatcher,
|
2011-03-21 19:56:33 +00:00
|
|
|
FormRendererInterface $renderer = null, DataTransformerInterface $clientTransformer = null,
|
2011-03-20 12:35:19 +00:00
|
|
|
DataTransformerInterface $normTransformer = null,
|
|
|
|
DataMapperInterface $dataMapper = null, array $validators = array(),
|
2011-03-20 09:35:16 +00:00
|
|
|
$required = false, $readOnly = false, array $attributes = array())
|
2011-03-01 13:19:28 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
foreach ($validators as $validator) {
|
|
|
|
if (!$validator instanceof FormValidatorInterface) {
|
|
|
|
throw new UnexpectedTypeException($validator, 'Symfony\Component\Form\Validator\FormValidatorInterface');
|
|
|
|
}
|
|
|
|
}
|
2011-03-16 17:20:13 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->name = (string)$name;
|
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
$this->renderer = $renderer;
|
|
|
|
$this->clientTransformer = $clientTransformer;
|
|
|
|
$this->normTransformer = $normTransformer;
|
|
|
|
$this->validators = $validators;
|
2011-03-18 11:50:26 +00:00
|
|
|
$this->dataMapper = $dataMapper;
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->required = $required;
|
|
|
|
$this->readOnly = $readOnly;
|
|
|
|
$this->attributes = $attributes;
|
2011-03-01 13:19:28 +00:00
|
|
|
|
2011-03-21 19:46:43 +00:00
|
|
|
if ($renderer) {
|
2011-03-21 19:56:33 +00:00
|
|
|
$renderer->setForm($this);
|
2011-03-21 19:46:43 +00:00
|
|
|
}
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
$this->setData(null);
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
2011-01-29 21:39:36 +00:00
|
|
|
/**
|
2011-03-20 12:35:19 +00:00
|
|
|
* Cloning is not supported
|
2011-01-29 21:39:36 +00:00
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
private function __clone()
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function getName()
|
2011-03-18 11:50:26 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
return $this->name;
|
|
|
|
}
|
2011-01-29 21:39:36 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isRequired()
|
|
|
|
{
|
|
|
|
if (null === $this->parent || $this->parent->isRequired()) {
|
|
|
|
return $this->required;
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isReadOnly()
|
|
|
|
{
|
|
|
|
if (null === $this->parent || !$this->parent->isReadOnly()) {
|
|
|
|
return $this->readOnly;
|
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
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function setParent(FormInterface $parent = null)
|
2011-01-29 21:39:36 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-03-20 12:35:19 +00:00
|
|
|
* Returns the parent field.
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return FormInterface The parent field
|
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-03-20 12:35:19 +00:00
|
|
|
* Returns whether the field 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
|
|
|
/**
|
|
|
|
* Returns the root of the form tree
|
|
|
|
*
|
|
|
|
* @return FormInterface The root of the tree
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Returns whether the field is the root of the form tree
|
|
|
|
*
|
|
|
|
* @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-03-20 12:35:19 +00:00
|
|
|
public function hasAttribute($name)
|
|
|
|
{
|
|
|
|
return isset($this->attributes[$name]);
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
public function getAttribute($name)
|
2011-03-18 11:50:26 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
return $this->attributes[$name];
|
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* Updates the field with default data
|
|
|
|
*
|
|
|
|
* @see FormInterface
|
|
|
|
*/
|
|
|
|
public function setData($appData)
|
|
|
|
{
|
|
|
|
$event = new DataEvent($this, $appData);
|
|
|
|
$this->dispatcher->dispatch(Events::preSetData, $event);
|
|
|
|
|
|
|
|
// Hook to change content of the data
|
|
|
|
$event = new FilterDataEvent($this, $appData);
|
|
|
|
$this->dispatcher->dispatch(Events::filterSetData, $event);
|
|
|
|
$appData = $event->getData();
|
|
|
|
|
|
|
|
// Fix data if empty
|
|
|
|
if (!$this->clientTransformer) {
|
|
|
|
if (empty($appData) && !$this->normTransformer && $this->dataMapper) {
|
|
|
|
$appData = $this->dataMapper->createEmptyData();
|
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
// Treat data as strings unless a value transformer exists
|
|
|
|
if (is_scalar($appData)) {
|
|
|
|
$appData = (string)$appData;
|
2011-03-18 11:50:26 +00:00
|
|
|
}
|
|
|
|
}
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
// Synchronize representations - must not change the content!
|
2011-03-20 14:22:37 +00:00
|
|
|
$normData = $this->appToNorm($appData);
|
|
|
|
$clientData = $this->normToClient($normData);
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
$this->data = $appData;
|
|
|
|
$this->normData = $normData;
|
|
|
|
$this->clientData = $clientData;
|
|
|
|
|
|
|
|
if ($this->dataMapper) {
|
|
|
|
// Update child forms from the data
|
|
|
|
$this->dataMapper->mapDataToForms($clientData, $this->children);
|
|
|
|
}
|
|
|
|
|
|
|
|
$event = new DataEvent($this, $appData);
|
|
|
|
$this->dispatcher->dispatch(Events::postSetData, $event);
|
|
|
|
|
|
|
|
return $this;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* Binds POST data to the field, transforms and validates it.
|
|
|
|
*
|
|
|
|
* @param string|array $data The POST data
|
|
|
|
*/
|
|
|
|
public function bind($clientData)
|
2011-03-01 13:19:28 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
if (is_scalar($clientData) || null === $clientData) {
|
|
|
|
$clientData = (string)$clientData;
|
|
|
|
}
|
2011-03-16 17:20:13 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
$event = new DataEvent($this, $clientData);
|
|
|
|
$this->dispatcher->dispatch(Events::preBind, $event);
|
|
|
|
|
|
|
|
$appData = null;
|
|
|
|
$normData = null;
|
|
|
|
$extraData = array();
|
|
|
|
|
|
|
|
// Hook to change content of the data bound by the browser
|
|
|
|
$event = new FilterDataEvent($this, $clientData);
|
|
|
|
$this->dispatcher->dispatch(Events::filterBoundClientData, $event);
|
|
|
|
$clientData = $event->getData();
|
|
|
|
|
|
|
|
if (count($this->children) > 0) {
|
|
|
|
if (empty($clientData)) {
|
|
|
|
$clientData = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_array($clientData)) {
|
|
|
|
throw new UnexpectedTypeException($clientData, 'array');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->children as $name => $child) {
|
|
|
|
if (!isset($clientData[$name])) {
|
|
|
|
$clientData[$name] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($clientData as $name => $value) {
|
|
|
|
if ($this->has($name)) {
|
|
|
|
$this->children[$name]->bind($value);
|
|
|
|
} else {
|
|
|
|
$extraData[$name] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge form data from children into existing client data
|
|
|
|
if ($this->dataMapper) {
|
|
|
|
$clientData = $this->getClientData();
|
|
|
|
|
|
|
|
$this->dataMapper->mapFormsToData($this->children, $clientData);
|
|
|
|
}
|
2011-03-19 14:06:54 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
try {
|
|
|
|
// Normalize data to unified representation
|
2011-03-20 14:22:37 +00:00
|
|
|
$normData = $this->clientToNorm($clientData);
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->transformationSuccessful = true;
|
|
|
|
} catch (TransformationFailedException $e) {
|
|
|
|
$this->transformationSuccessful = false;
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
if ($this->transformationSuccessful) {
|
|
|
|
// Hook to change content of the data in the normalized
|
|
|
|
// representation
|
|
|
|
$event = new FilterDataEvent($this, $normData);
|
|
|
|
$this->dispatcher->dispatch(Events::filterBoundNormData, $event);
|
|
|
|
$normData = $event->getData();
|
|
|
|
|
|
|
|
// Synchronize representations - must not change the content!
|
2011-03-20 14:22:37 +00:00
|
|
|
$appData = $this->normToApp($normData);
|
|
|
|
$clientData = $this->normToClient($normData);
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->bound = true;
|
|
|
|
$this->errors = array();
|
|
|
|
$this->data = $appData;
|
|
|
|
$this->normData = $normData;
|
|
|
|
$this->clientData = $clientData;
|
|
|
|
$this->extraData = $extraData;
|
2011-03-19 18:53:39 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
$event = new DataEvent($this, $clientData);
|
|
|
|
$this->dispatcher->dispatch(Events::postBind, $event);
|
|
|
|
|
|
|
|
foreach ($this->validators as $validator) {
|
|
|
|
$validator->validate($this);
|
2011-03-01 13:19:28 +00:00
|
|
|
}
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
2011-03-01 13:19:28 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the data in the format needed for the underlying object.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getData()
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
2011-03-01 13:19:28 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the normalized data of the field.
|
|
|
|
*
|
|
|
|
* @return mixed When the field is not bound, the default data is returned.
|
|
|
|
* When the field is bound, the normalized bound data is
|
|
|
|
* returned if the field is valid, null otherwise.
|
|
|
|
*/
|
|
|
|
public function getNormData()
|
|
|
|
{
|
|
|
|
return $this->normData;
|
|
|
|
}
|
2011-03-18 11:50:26 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* Returns the data transformed by the value transformer
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getClientData()
|
|
|
|
{
|
|
|
|
return $this->clientData;
|
2011-02-24 11:22:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 11:00:19 +00:00
|
|
|
public function getExtraData()
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-20 11:00:19 +00:00
|
|
|
return $this->extraData;
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
/**
|
|
|
|
* Adds an error to the field.
|
|
|
|
*
|
|
|
|
* @see FormInterface
|
|
|
|
*/
|
2011-03-21 20:01:14 +00:00
|
|
|
public function addError(FormError $error)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
|
|
|
$this->errors[] = $error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the field is bound.
|
|
|
|
*
|
|
|
|
* @return Boolean true if the form is bound to input values, false otherwise
|
|
|
|
*/
|
|
|
|
public function isBound()
|
|
|
|
{
|
|
|
|
return $this->bound;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the bound value could be reverse transformed correctly
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function isTransformationSuccessful()
|
|
|
|
{
|
|
|
|
return $this->transformationSuccessful;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
foreach ($this->children as $child) {
|
|
|
|
if (!$child->isEmpty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array() === $this->data || null === $this->data || '' === $this->data;
|
|
|
|
}
|
|
|
|
|
2011-01-29 21:39:36 +00:00
|
|
|
/**
|
|
|
|
* Returns whether the field is valid.
|
|
|
|
*
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function isValid()
|
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
// TESTME
|
|
|
|
if (!$this->isBound() || $this->hasErrors()) {
|
2011-01-29 21:39:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
foreach ($this->children as $child) {
|
|
|
|
if (!$child->isValid()) {
|
2011-01-29 21:39:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return Boolean true if form is bound and not valid
|
|
|
|
*/
|
|
|
|
public function hasErrors()
|
|
|
|
{
|
|
|
|
// Don't call isValid() here, as its semantics are slightly different
|
|
|
|
// Field groups are not valid if their children are invalid, but
|
|
|
|
// hasErrors() returns only true if a field/field group itself has
|
|
|
|
// errors
|
|
|
|
return count($this->errors) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all errors
|
|
|
|
*
|
|
|
|
* @return array An array of FormError instances that occurred during binding
|
|
|
|
*/
|
|
|
|
public function getErrors()
|
|
|
|
{
|
|
|
|
return $this->errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the DataTransformer.
|
|
|
|
*
|
|
|
|
* @return DataTransformerInterface
|
|
|
|
*/
|
|
|
|
public function getNormTransformer()
|
|
|
|
{
|
|
|
|
return $this->normTransformer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the DataTransformer.
|
|
|
|
*
|
|
|
|
* @return DataTransformerInterface
|
|
|
|
*/
|
|
|
|
public function getClientTransformer()
|
|
|
|
{
|
|
|
|
return $this->clientTransformer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the renderer
|
|
|
|
*
|
2011-03-21 19:56:33 +00:00
|
|
|
* @return FormRendererInterface
|
2011-03-20 12:35:19 +00:00
|
|
|
*/
|
|
|
|
public function getRenderer()
|
|
|
|
{
|
|
|
|
return $this->renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all children in this group
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getChildren()
|
|
|
|
{
|
|
|
|
return $this->children;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function add(FormInterface $child)
|
|
|
|
{
|
|
|
|
$this->children[$child->getName()] = $child;
|
|
|
|
|
|
|
|
$child->setParent($this);
|
|
|
|
|
|
|
|
$this->dataMapper->mapDataToForm($this->getClientData(), $child);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function remove($name)
|
|
|
|
{
|
|
|
|
if (isset($this->children[$name])) {
|
|
|
|
$this->children[$name]->setParent(null);
|
|
|
|
|
|
|
|
unset($this->children[$name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a child with the given name exists.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
|
|
|
return isset($this->children[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the child with the given name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return FormInterface
|
|
|
|
*/
|
|
|
|
public function get($name)
|
|
|
|
{
|
|
|
|
if (isset($this->children[$name])) {
|
|
|
|
return $this->children[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(sprintf('Field "%s" does not exist.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return Field A form child 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws an exception saying that values cannot be set (implements the \ArrayAccess interface).
|
|
|
|
*
|
|
|
|
* @param string $offset (ignored)
|
|
|
|
* @param string $value (ignored)
|
|
|
|
*
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
2011-03-20 12:35:19 +00:00
|
|
|
public function offsetSet($name, $child)
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-18 11:50:26 +00:00
|
|
|
throw new \BadMethodCallException('offsetSet() is not supported');
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Throws an exception saying that values cannot be unset (implements the \ArrayAccess interface).
|
|
|
|
*
|
2011-03-17 10:03:10 +00:00
|
|
|
* @param string $name
|
2011-01-29 21:39:36 +00:00
|
|
|
*
|
|
|
|
* @throws \LogicException
|
|
|
|
*/
|
2011-03-17 10:03:10 +00:00
|
|
|
public function offsetUnset($name)
|
2011-01-29 21:39:36 +00:00
|
|
|
{
|
2011-03-18 11:50:26 +00:00
|
|
|
throw new \BadMethodCallException('offsetUnset() is not supported');
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Normalizes the value if a normalization transformer is set
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to transform
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-03-20 14:22:37 +00:00
|
|
|
private function appToNorm($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
|
|
|
if (null === $this->normTransformer) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->normTransformer->transform($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse transforms a value if a normalization transformer is set.
|
|
|
|
*
|
|
|
|
* @param string $value The value to reverse transform
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2011-03-20 14:22:37 +00:00
|
|
|
private function normToApp($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
|
|
|
if (null === $this->normTransformer) {
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->normTransformer->reverseTransform($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms the value if a value transformer is set.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to transform
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-03-20 14:22:37 +00:00
|
|
|
private function normToClient($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
|
|
|
if (null === $this->clientTransformer) {
|
|
|
|
// Scalar values should always be converted to strings to
|
|
|
|
// facilitate differentiation between empty ("") and zero (0).
|
|
|
|
return null === $value || is_scalar($value) ? (string)$value : $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->clientTransformer->transform($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reverse transforms a value if a value transformer is set.
|
|
|
|
*
|
|
|
|
* @param string $value The value to reverse transform
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2011-03-20 14:22:37 +00:00
|
|
|
private function clientToNorm($value)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
|
|
|
if (null === $this->clientTransformer) {
|
|
|
|
return '' === $value ? null : $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->clientTransformer->reverseTransform($value);
|
2011-01-29 21:39:36 +00:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
2011-02-01 09:59:18 +00:00
|
|
|
* Binds a request to the form
|
2010-06-24 10:24:08 +01:00
|
|
|
*
|
2011-03-17 13:55:11 +00:00
|
|
|
* If the request was a POST request, the data is bound to the form,
|
2011-02-01 09:59:18 +00:00
|
|
|
* transformed and written into the form data (an object or an array).
|
|
|
|
* You can set the form data by passing it in the second parameter
|
|
|
|
* of this method or by passing it in the "data" option of the form's
|
|
|
|
* constructor.
|
|
|
|
*
|
|
|
|
* @param Request $request The request to bind to the form
|
|
|
|
* @param array|object $data The data from which to read default values
|
2011-03-17 13:55:11 +00:00
|
|
|
* and where to write bound values
|
2011-01-28 22:09:22 +00:00
|
|
|
*/
|
2011-03-17 13:51:22 +00:00
|
|
|
public function bindRequest(Request $request)
|
2011-01-28 22:09:22 +00:00
|
|
|
{
|
2011-03-17 13:55:11 +00:00
|
|
|
// Store the bound data in case of a post request
|
2011-03-17 13:51:22 +00:00
|
|
|
switch ($request->getMethod()) {
|
|
|
|
case 'POST':
|
|
|
|
case 'PUT':
|
|
|
|
$data = array_replace_recursive(
|
|
|
|
$request->request->get($this->getName(), array()),
|
|
|
|
$request->files->get($this->getName(), array())
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'GET':
|
|
|
|
$data = $request->query->get($this->getName(), array());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new FormException(sprintf('The request method "%s" is not supported', $request->getMethod()));
|
2011-02-01 09:59:18 +00:00
|
|
|
}
|
2011-03-17 13:51:22 +00:00
|
|
|
|
2011-03-17 13:55:11 +00:00
|
|
|
$this->bind($data);
|
2011-01-28 22:09:22 +00:00
|
|
|
}
|
2010-09-09 14:23:28 +01:00
|
|
|
|
2011-02-03 09:53:51 +00:00
|
|
|
/**
|
|
|
|
* Validates the data of this form
|
|
|
|
*
|
|
|
|
* This method is called automatically during the validation process.
|
|
|
|
*
|
|
|
|
* @param ExecutionContext $context The current validation context
|
2011-03-20 12:35:19 +00:00
|
|
|
* @deprecated
|
2011-02-03 09:53:51 +00:00
|
|
|
*/
|
|
|
|
public function validateData(ExecutionContext $context)
|
|
|
|
{
|
2011-02-04 09:12:11 +00:00
|
|
|
if (is_object($this->getData()) || is_array($this->getData())) {
|
2011-03-18 14:26:02 +00:00
|
|
|
$groups = $this->getAttribute('validation_groups');
|
2011-03-20 12:35:19 +00:00
|
|
|
$child = $this;
|
2011-03-18 14:26:02 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
while (!$groups && $child->hasParent()) {
|
|
|
|
$child = $child->getParent();
|
|
|
|
$groups = $child->getAttribute('validation_groups');
|
2011-03-18 14:26:02 +00:00
|
|
|
}
|
2011-02-03 09:53:51 +00:00
|
|
|
|
2011-02-04 09:12:11 +00:00
|
|
|
if (null === $groups) {
|
|
|
|
$groups = array(null);
|
|
|
|
}
|
2011-02-03 09:53:51 +00:00
|
|
|
|
2011-03-18 14:26:02 +00:00
|
|
|
$propertyPath = $context->getPropertyPath();
|
|
|
|
$graphWalker = $context->getGraphWalker();
|
|
|
|
|
2011-02-04 09:12:11 +00:00
|
|
|
// The Execute constraint is called on class level, so we need to
|
|
|
|
// set the property manually
|
|
|
|
$context->setCurrentProperty('data');
|
2011-02-03 09:53:51 +00:00
|
|
|
|
2011-02-04 09:12:11 +00:00
|
|
|
// Adjust the property path accordingly
|
|
|
|
if (!empty($propertyPath)) {
|
|
|
|
$propertyPath .= '.';
|
|
|
|
}
|
2011-02-03 09:53:51 +00:00
|
|
|
|
2011-02-04 09:12:11 +00:00
|
|
|
$propertyPath .= 'data';
|
2011-02-03 09:53:51 +00:00
|
|
|
|
2011-02-04 09:12:11 +00:00
|
|
|
foreach ($groups as $group) {
|
|
|
|
$graphWalker->walkReference($this->getData(), $group, $propertyPath, true);
|
|
|
|
}
|
2011-02-03 09:53:51 +00:00
|
|
|
}
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|