2011-03-18 15:34:54 +00:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
use Symfony\Component\Form\DataMapper\DataMapperInterface;
|
2011-03-20 12:35:19 +00:00
|
|
|
use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
|
|
|
|
use Symfony\Component\Form\Validator\FormValidatorInterface;
|
2011-03-19 13:31:25 +00:00
|
|
|
use Symfony\Component\Form\Exception\FormException;
|
2011-03-19 18:20:55 +00:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
2011-03-27 13:05:06 +01:00
|
|
|
use Symfony\Component\Form\Type\FormTypeInterface;
|
2011-03-18 15:34:54 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2011-03-20 12:35:19 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
class FormBuilder
|
2011-03-18 15:34:54 +00:00
|
|
|
{
|
2011-03-20 12:35:19 +00:00
|
|
|
private $name;
|
|
|
|
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
private $dispatcher;
|
|
|
|
|
|
|
|
private $factory;
|
|
|
|
|
|
|
|
private $readOnly;
|
|
|
|
|
|
|
|
private $required;
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
private $clientTransformers = array();
|
2011-03-20 12:35:19 +00:00
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
private $normTransformers = array();
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
private $validators = array();
|
|
|
|
|
|
|
|
private $attributes = array();
|
|
|
|
|
2011-03-24 15:16:31 +00:00
|
|
|
private $types = array();
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
private $parent;
|
|
|
|
|
2011-03-18 15:34:54 +00:00
|
|
|
private $dataClass;
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
private $children = array();
|
2011-03-18 15:34:54 +00:00
|
|
|
|
|
|
|
private $dataMapper;
|
|
|
|
|
2011-03-21 21:10:53 +00:00
|
|
|
private $errorBubbling = false;
|
|
|
|
|
2011-04-06 12:45:19 +01:00
|
|
|
private $emptyData = '';
|
2011-03-27 14:12:35 +01:00
|
|
|
|
2011-03-31 12:48:06 +01:00
|
|
|
public function __construct($name, EventDispatcherInterface $dispatcher, $dataClass = null)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2011-03-31 12:48:06 +01:00
|
|
|
$this->name = $name;
|
2011-03-20 12:35:19 +00:00
|
|
|
$this->dispatcher = $dispatcher;
|
2011-03-27 13:34:48 +01:00
|
|
|
$this->dataClass = $dataClass;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setFormFactory(FormFactoryInterface $factory)
|
|
|
|
{
|
|
|
|
$this->factory = $factory;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormFactory()
|
|
|
|
{
|
|
|
|
return $this->factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setParent(FormBuilder $builder)
|
|
|
|
{
|
|
|
|
$this->parent = $builder;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getParent()
|
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function end()
|
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setData($data)
|
|
|
|
{
|
|
|
|
$this->data = $data;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getData()
|
|
|
|
{
|
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setReadOnly($readOnly)
|
|
|
|
{
|
|
|
|
$this->readOnly = $readOnly;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getReadOnly()
|
|
|
|
{
|
|
|
|
return $this->readOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether this field is required to be filled out when bound.
|
|
|
|
*
|
|
|
|
* @param Boolean $required
|
|
|
|
*/
|
|
|
|
public function setRequired($required)
|
|
|
|
{
|
|
|
|
$this->required = $required;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRequired()
|
|
|
|
{
|
|
|
|
return $this->required;
|
|
|
|
}
|
|
|
|
|
2011-03-21 21:10:53 +00:00
|
|
|
public function setErrorBubbling($errorBubbling)
|
|
|
|
{
|
|
|
|
$this->errorBubbling = $errorBubbling;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getErrorBubbling()
|
|
|
|
{
|
|
|
|
return $this->errorBubbling;
|
|
|
|
}
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
public function addValidator(FormValidatorInterface $validator)
|
|
|
|
{
|
|
|
|
$this->validators[] = $validator;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getValidators()
|
|
|
|
{
|
|
|
|
return $this->validators;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an event listener for events on this field
|
|
|
|
*
|
|
|
|
* @see Symfony\Component\EventDispatcher\EventDispatcherInterface::addEventListener
|
|
|
|
*/
|
|
|
|
public function addEventListener($eventNames, $listener, $priority = 0)
|
|
|
|
{
|
|
|
|
$this->dispatcher->addListener($eventNames, $listener, $priority);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an event subscriber for events on this field
|
|
|
|
*
|
|
|
|
* @see Symfony\Component\EventDispatcher\EventDispatcherInterface::addEventSubscriber
|
|
|
|
*/
|
|
|
|
public function addEventSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
|
|
|
|
{
|
|
|
|
$this->dispatcher->addSubscriber($subscriber, $priority);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
/**
|
|
|
|
* Appends a transformer to the normalization transformer chain
|
|
|
|
*
|
|
|
|
* @param DataTransformerInterface $clientTransformer
|
|
|
|
*/
|
|
|
|
public function appendNormTransformer(DataTransformerInterface $normTransformer = null)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2011-04-02 15:39:19 +01:00
|
|
|
$this->normTransformers[] = $normTransformer;
|
|
|
|
|
|
|
|
return $this;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-02 15:39:19 +01:00
|
|
|
* Prepends a transformer to the client transformer chain
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
|
|
|
* @param DataTransformerInterface $clientTransformer
|
|
|
|
*/
|
2011-04-02 15:39:19 +01:00
|
|
|
public function prependNormTransformer(DataTransformerInterface $normTransformer = null)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2011-04-02 15:39:19 +01:00
|
|
|
array_unshift($this->normTransformers, $normTransformer);
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
public function resetNormTransformers()
|
|
|
|
{
|
|
|
|
$this->normTransformers = array();
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNormTransformers()
|
|
|
|
{
|
|
|
|
return $this->normTransformers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends a transformer to the client transformer chain
|
|
|
|
*
|
|
|
|
* @param DataTransformerInterface $clientTransformer
|
|
|
|
*/
|
|
|
|
public function appendClientTransformer(DataTransformerInterface $clientTransformer = null)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2011-04-02 15:39:19 +01:00
|
|
|
$this->clientTransformers[] = $clientTransformer;
|
|
|
|
|
|
|
|
return $this;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-02 15:39:19 +01:00
|
|
|
* Prepends a transformer to the client transformer chain
|
2011-03-20 12:35:19 +00:00
|
|
|
*
|
|
|
|
* @param DataTransformerInterface $clientTransformer
|
|
|
|
*/
|
2011-04-02 15:39:19 +01:00
|
|
|
public function prependClientTransformer(DataTransformerInterface $clientTransformer = null)
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2011-04-02 15:39:19 +01:00
|
|
|
array_unshift($this->clientTransformers, $clientTransformer);
|
2011-03-20 12:35:19 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
public function resetClientTransformers()
|
2011-03-20 12:35:19 +00:00
|
|
|
{
|
2011-04-02 15:39:19 +01:00
|
|
|
$this->clientTransformers = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClientTransformers()
|
|
|
|
{
|
|
|
|
return $this->clientTransformers;
|
2011-03-20 12:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function setAttribute($name, $value)
|
|
|
|
{
|
|
|
|
$this->attributes[$name] = $value;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAttribute($name)
|
|
|
|
{
|
|
|
|
return $this->attributes[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function hasAttribute($name)
|
|
|
|
{
|
|
|
|
return isset($this->attributes[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAttributes()
|
|
|
|
{
|
|
|
|
return $this->attributes;
|
|
|
|
}
|
|
|
|
|
2011-03-18 15:34:54 +00:00
|
|
|
public function setDataMapper(DataMapperInterface $dataMapper)
|
|
|
|
{
|
|
|
|
$this->dataMapper = $dataMapper;
|
2011-04-02 10:22:29 +01:00
|
|
|
|
|
|
|
return $this;
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDataMapper()
|
|
|
|
{
|
|
|
|
return $this->dataMapper;
|
|
|
|
}
|
|
|
|
|
2011-03-24 15:16:31 +00:00
|
|
|
public function setTypes(array $types)
|
|
|
|
{
|
|
|
|
$this->types = $types;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTypes()
|
|
|
|
{
|
|
|
|
return $this->types;
|
|
|
|
}
|
|
|
|
|
2011-03-27 14:12:35 +01:00
|
|
|
public function setEmptyData($emptyData)
|
|
|
|
{
|
|
|
|
$this->emptyData = $emptyData;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEmptyData()
|
|
|
|
{
|
|
|
|
return $this->emptyData;
|
|
|
|
}
|
|
|
|
|
2011-03-18 15:34:54 +00:00
|
|
|
/**
|
|
|
|
* Adds a new field to this group. A field must have a unique name within
|
|
|
|
* the group. Otherwise the existing field is overwritten.
|
|
|
|
*
|
|
|
|
* If you add a nested group, this group should also be represented in the
|
|
|
|
* object hierarchy. If you want to add a group that operates on the same
|
|
|
|
* hierarchy level, use merge().
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* class Entity
|
|
|
|
* {
|
|
|
|
* public $location;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* class Location
|
|
|
|
* {
|
|
|
|
* public $longitude;
|
|
|
|
* public $latitude;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* $entity = new Entity();
|
|
|
|
* $entity->location = new Location();
|
|
|
|
*
|
|
|
|
* $form = new Form('entity', $entity, $validator);
|
|
|
|
*
|
|
|
|
* $locationGroup = new Form('location');
|
|
|
|
* $locationGroup->add(new TextField('longitude'));
|
|
|
|
* $locationGroup->add(new TextField('latitude'));
|
|
|
|
*
|
|
|
|
* $form->add($locationGroup);
|
|
|
|
* </code>
|
|
|
|
*
|
2011-03-25 01:12:01 +00:00
|
|
|
* @param FormInterface|string $form
|
2011-03-20 12:35:19 +00:00
|
|
|
* @return FormInterface
|
2011-03-18 15:34:54 +00:00
|
|
|
*/
|
2011-03-19 18:20:55 +00:00
|
|
|
public function add($name, $type = null, array $options = array())
|
2011-03-18 15:34:54 +00:00
|
|
|
{
|
2011-03-19 18:20:55 +00:00
|
|
|
if (!is_string($name)) {
|
|
|
|
throw new UnexpectedTypeException($name, 'string');
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
2011-03-27 13:05:06 +01:00
|
|
|
if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
|
|
|
|
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\Type\FormTypeInterface');
|
2011-03-19 18:42:16 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
$this->children[$name] = array(
|
2011-03-19 18:42:16 +00:00
|
|
|
'type' => $type,
|
|
|
|
'options' => $options,
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function build($name, $type = null, array $options = array())
|
|
|
|
{
|
2011-03-19 18:20:55 +00:00
|
|
|
if (null !== $type) {
|
2011-03-19 18:42:16 +00:00
|
|
|
$builder = $this->getFormFactory()->createBuilder(
|
|
|
|
$type,
|
|
|
|
$name,
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (!$this->dataClass) {
|
2011-03-25 01:12:01 +00:00
|
|
|
throw new FormException('The data class must be set to automatically create children');
|
2011-03-19 18:20:55 +00:00
|
|
|
}
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-19 18:42:16 +00:00
|
|
|
$builder = $this->getFormFactory()->createBuilderForProperty(
|
|
|
|
$this->dataClass,
|
|
|
|
$name,
|
|
|
|
$options
|
2011-03-18 15:34:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
$this->children[$name] = $builder;
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-19 18:42:16 +00:00
|
|
|
$builder->setParent($this);
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-19 18:42:16 +00:00
|
|
|
return $builder;
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get($name)
|
|
|
|
{
|
2011-03-25 01:12:01 +00:00
|
|
|
if (!isset($this->children[$name])) {
|
2011-03-18 15:34:54 +00:00
|
|
|
throw new FormException(sprintf('The field "%s" does not exist', $name));
|
|
|
|
}
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
$child = $this->children[$name];
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
if ($child instanceof FormBuilder) {
|
|
|
|
return $child;
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
return $this->build($name, $child['type'], $child['options']);
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the field with the given name.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
2011-03-25 01:12:01 +00:00
|
|
|
if (isset($this->children[$name])) {
|
2011-03-19 21:03:49 +00:00
|
|
|
// field might still be lazy
|
2011-03-25 01:12:01 +00:00
|
|
|
if ($this->children[$name] instanceof FormInterface) {
|
|
|
|
$this->children[$name]->setParent(null);
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
unset($this->children[$name]);
|
2011-03-19 21:03:49 +00:00
|
|
|
}
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether a field with the given name exists.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @return Boolean
|
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
2011-03-25 01:12:01 +00:00
|
|
|
return isset($this->children[$name]);
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
2011-04-02 15:39:19 +01:00
|
|
|
protected function buildDispatcher()
|
|
|
|
{
|
|
|
|
return $this->dispatcher;
|
|
|
|
}
|
|
|
|
|
2011-03-20 14:16:53 +00:00
|
|
|
protected function buildChildren()
|
2011-03-18 15:34:54 +00:00
|
|
|
{
|
2011-03-25 01:12:01 +00:00
|
|
|
$children = array();
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
foreach ($this->children as $name => $builder) {
|
2011-03-20 12:35:19 +00:00
|
|
|
if (!$builder instanceof FormBuilder) {
|
2011-03-19 18:42:16 +00:00
|
|
|
$builder = $this->build($name, $builder['type'], $builder['options']);
|
|
|
|
}
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
$children[$builder->getName()] = $builder->getForm();
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
return $children;
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
2011-03-20 14:16:53 +00:00
|
|
|
public function getForm()
|
2011-03-18 15:34:54 +00:00
|
|
|
{
|
|
|
|
$instance = new Form(
|
|
|
|
$this->getName(),
|
|
|
|
$this->buildDispatcher(),
|
2011-03-31 14:23:33 +01:00
|
|
|
$this->getTypes(),
|
2011-04-02 15:39:19 +01:00
|
|
|
$this->getClientTransformers(),
|
|
|
|
$this->getNormTransformers(),
|
2011-03-18 15:34:54 +00:00
|
|
|
$this->getDataMapper(),
|
2011-03-20 11:00:19 +00:00
|
|
|
$this->getValidators(),
|
2011-03-18 15:34:54 +00:00
|
|
|
$this->getRequired(),
|
2011-03-20 09:35:16 +00:00
|
|
|
$this->getReadOnly(),
|
2011-03-21 21:10:53 +00:00
|
|
|
$this->getErrorBubbling(),
|
2011-03-27 14:12:35 +01:00
|
|
|
$this->getEmptyData(),
|
2011-03-18 15:34:54 +00:00
|
|
|
$this->getAttributes()
|
|
|
|
);
|
|
|
|
|
2011-03-25 01:12:01 +00:00
|
|
|
foreach ($this->buildChildren() as $child) {
|
|
|
|
$instance->add($child);
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->getData()) {
|
|
|
|
$instance->setData($this->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
}
|