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\Renderer\DefaultRenderer;
|
|
|
|
use Symfony\Component\Form\Renderer\RendererInterface;
|
|
|
|
use Symfony\Component\Form\Renderer\Plugin\RendererPluginInterface;
|
2011-03-18 15:34:54 +00:00
|
|
|
use Symfony\Component\Form\Renderer\Theme\ThemeInterface;
|
|
|
|
use Symfony\Component\Form\CsrfProvider\CsrfProviderInterface;
|
2011-03-20 12:35:19 +00:00
|
|
|
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-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;
|
|
|
|
|
|
|
|
private $renderer;
|
|
|
|
|
|
|
|
private $rendererVars = array();
|
|
|
|
|
|
|
|
private $clientTransformer;
|
|
|
|
|
|
|
|
private $normalizationTransformer;
|
|
|
|
|
|
|
|
private $theme;
|
|
|
|
|
|
|
|
private $validators = array();
|
|
|
|
|
|
|
|
private $attributes = array();
|
|
|
|
|
|
|
|
private $parent;
|
|
|
|
|
2011-03-18 15:34:54 +00:00
|
|
|
private $dataClass;
|
|
|
|
|
|
|
|
private $csrfFieldName;
|
|
|
|
|
|
|
|
private $csrfProvider;
|
|
|
|
|
|
|
|
private $fields = array();
|
|
|
|
|
|
|
|
private $dataMapper;
|
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
public function __construct(ThemeInterface $theme,
|
|
|
|
EventDispatcherInterface $dispatcher)
|
|
|
|
{
|
|
|
|
$this->theme = $theme;
|
|
|
|
$this->dispatcher = $dispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFormFactory(FormFactoryInterface $factory)
|
|
|
|
{
|
|
|
|
$this->factory = $factory;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFormFactory()
|
|
|
|
{
|
|
|
|
return $this->factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildDispatcher()
|
|
|
|
{
|
|
|
|
return $this->dispatcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the DataTransformer.
|
|
|
|
*
|
|
|
|
* @param DataTransformerInterface $clientTransformer
|
|
|
|
*/
|
|
|
|
public function setNormTransformer(DataTransformerInterface $normalizationTransformer = null)
|
|
|
|
{
|
|
|
|
$this->normalizationTransformer = $normalizationTransformer;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getNormTransformer()
|
|
|
|
{
|
|
|
|
return $this->normalizationTransformer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the DataTransformer.
|
|
|
|
*
|
|
|
|
* @param DataTransformerInterface $clientTransformer
|
|
|
|
*/
|
|
|
|
public function setClientTransformer(DataTransformerInterface $clientTransformer = null)
|
|
|
|
{
|
|
|
|
$this->clientTransformer = $clientTransformer;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getClientTransformer()
|
|
|
|
{
|
|
|
|
return $this->clientTransformer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the renderer
|
|
|
|
*
|
|
|
|
* @param RendererInterface $renderer
|
|
|
|
*/
|
|
|
|
public function setRenderer(RendererInterface $renderer)
|
|
|
|
{
|
|
|
|
$this->renderer = $renderer;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addRendererPlugin(RendererPluginInterface $plugin)
|
|
|
|
{
|
|
|
|
$this->rendererVars[] = $plugin;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setRendererVar($name, $value)
|
|
|
|
{
|
|
|
|
$this->rendererVars[$name] = $value;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildRenderer()
|
|
|
|
{
|
|
|
|
if (!$this->renderer) {
|
|
|
|
$this->renderer = new DefaultRenderer($this->theme, 'text');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->rendererVars as $name => $value) {
|
|
|
|
if ($value instanceof RendererPluginInterface) {
|
|
|
|
$this->renderer->addPlugin($value);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->renderer->setVar($name, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->renderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDataMapper()
|
|
|
|
{
|
|
|
|
return $this->dataMapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-20 12:35:19 +00:00
|
|
|
* @param FormInterface|string $field
|
|
|
|
* @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-19 18:42:16 +00:00
|
|
|
if (null !== $type && !is_string($type)) {
|
|
|
|
throw new UnexpectedTypeException($type, 'string');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fields[$name] = array(
|
|
|
|
'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) {
|
|
|
|
throw new FormException('The data class must be set to automatically create fields');
|
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-19 18:42:16 +00:00
|
|
|
$this->fields[$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)
|
|
|
|
{
|
|
|
|
if (!isset($this->fields[$name])) {
|
|
|
|
throw new FormException(sprintf('The field "%s" does not exist', $name));
|
|
|
|
}
|
|
|
|
|
2011-03-19 18:42:16 +00:00
|
|
|
$field = $this->fields[$name];
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-20 12:35:19 +00:00
|
|
|
if ($field instanceof FormBuilder) {
|
2011-03-19 18:42:16 +00:00
|
|
|
return $field;
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
2011-03-19 18:42:16 +00:00
|
|
|
return $this->build($name, $field['type'], $field['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-19 21:03:49 +00:00
|
|
|
if (isset($this->fields[$name])) {
|
|
|
|
// field might still be lazy
|
2011-03-20 12:35:19 +00:00
|
|
|
if ($this->fields[$name] instanceof FormInterface) {
|
2011-03-19 21:03:49 +00:00
|
|
|
$this->fields[$name]->setParent(null);
|
|
|
|
}
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-19 21:03:49 +00:00
|
|
|
unset($this->fields[$name]);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
return isset($this->fields[$name]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildFields()
|
|
|
|
{
|
|
|
|
$fields = array();
|
|
|
|
|
2011-03-19 18:42:16 +00:00
|
|
|
foreach ($this->fields 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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields[$name] = $builder->getInstance();
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addCsrfProtection(CsrfProviderInterface $provider = null, $fieldName = '_token')
|
|
|
|
{
|
|
|
|
if (null !== $provider) {
|
|
|
|
$this->csrfProvider = $provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->csrfFieldName = $fieldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeCsrfProtection()
|
|
|
|
{
|
|
|
|
$this->csrfFieldName = null;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if this form is CSRF protected
|
|
|
|
*/
|
|
|
|
public function hasCsrfProtection()
|
|
|
|
{
|
|
|
|
return isset($this->csrfFieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCsrfFieldName()
|
|
|
|
{
|
|
|
|
return $this->csrfFieldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCsrfProvider()
|
|
|
|
{
|
|
|
|
return $this->csrfProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function buildCsrfProtection()
|
|
|
|
{
|
|
|
|
if ($this->hasCsrfProtection()) {
|
2011-03-19 14:06:54 +00:00
|
|
|
// need a page ID here, maybe FormType class?
|
|
|
|
$options = array('page_id' => null);
|
|
|
|
|
|
|
|
if ($this->csrfProvider) {
|
|
|
|
$options['csrf_provider'] = $this->csrfProvider;
|
|
|
|
}
|
2011-03-18 15:34:54 +00:00
|
|
|
|
2011-03-19 18:20:55 +00:00
|
|
|
$this->add($this->csrfFieldName, 'csrf', $options);
|
2011-03-18 15:34:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDataClass($class)
|
|
|
|
{
|
|
|
|
$this->dataClass = $class;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDataClass()
|
|
|
|
{
|
|
|
|
return $this->dataClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInstance()
|
|
|
|
{
|
|
|
|
$this->buildCsrfProtection();
|
|
|
|
|
|
|
|
$instance = new Form(
|
|
|
|
$this->getName(),
|
|
|
|
$this->buildDispatcher(),
|
|
|
|
$this->buildRenderer(),
|
2011-03-18 15:59:34 +00:00
|
|
|
$this->getClientTransformer(),
|
|
|
|
$this->getNormTransformer(),
|
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-18 15:34:54 +00:00
|
|
|
$this->getAttributes()
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($this->buildFields() as $field) {
|
|
|
|
$instance->add($field);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->getData()) {
|
|
|
|
$instance->setData($this->getData());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instance;
|
|
|
|
}
|
|
|
|
}
|