123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- <?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\Exception\FormException;
- use Symfony\Component\Form\Exception\UnexpectedTypeException;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- class FormBuilder
- {
- /**
- * @var string
- */
- private $name;
- /**
- * The form data in application format
- * @var mixed
- */
- private $appData;
- /**
- * The event dispatcher
- *
- * @var EventDispatcherInterface
- */
- private $dispatcher;
- /**
- * The form factory
- * @var FormFactoryInterface
- */
- private $factory;
- /**
- * @var Boolean
- */
- private $readOnly;
- /**
- * @var Boolean
- */
- private $required;
- /**
- * The transformers for transforming from normalized to client format and
- * back
- * @var array An array of DataTransformerInterface
- */
- private $clientTransformers = array();
- /**
- * The transformers for transforming from application to normalized format
- * and back
- * @var array An array of DataTransformerInterface
- */
- private $normTransformers = array();
- /**
- * @var array An array of FormValidatorInterface
- */
- private $validators = array();
- /**
- * Key-value store for arbitrary attributes attached to the form
- * @var array
- */
- private $attributes = array();
- /**
- * @var array An array of FormTypeInterface
- */
- private $types = array();
- /**
- * @var string
- */
- private $dataClass;
- /**
- * The children of the form
- * @var array
- */
- private $children = array();
- /**
- * @var DataMapperInterface
- */
- private $dataMapper;
- /**
- * Whether added errors should bubble up to the parent
- * @var Boolean
- */
- private $errorBubbling = false;
- /**
- * Data used for the client data when no value is bound
- * @var mixed
- */
- private $emptyData = '';
- /**
- * Constructor.
- *
- * @param string $name
- * @param FormFactoryInterface $factory
- * @param EventDispatcherInterface $dispatcher
- * @param string $dataClass
- */
- public function __construct($name, FormFactoryInterface $factory, EventDispatcherInterface $dispatcher, $dataClass = null)
- {
- $this->name = $name;
- $this->factory = $factory;
- $this->dispatcher = $dispatcher;
- $this->dataClass = $dataClass;
- }
- /**
- * Returns the associated form factory.
- *
- * @return FormFactoryInterface The factory
- */
- public function getFormFactory()
- {
- return $this->factory;
- }
- /**
- * Returns the name of the form.
- *
- * @return string The form name
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * Updates the field with default data.
- *
- * @param array $appData The data formatted as expected for the underlying object
- *
- * @return FormBuilder The current builder
- */
- public function setData($appData)
- {
- $this->appData = $appData;
- return $this;
- }
- /**
- * Returns the data in the format needed for the underlying object.
- *
- * @return mixed
- */
- public function getData()
- {
- return $this->appData;
- }
- /**
- * Set whether the form is read only
- *
- * @param Boolean $readOnly Whether the form is read only
- *
- * @return FormBuilder The current builder
- */
- public function setReadOnly($readOnly)
- {
- $this->readOnly = (Boolean) $readOnly;
- return $this;
- }
- /**
- * Returns whether the form is read only.
- *
- * @return Boolean Whether the form is read only
- */
- public function getReadOnly()
- {
- return $this->readOnly;
- }
- /**
- * Sets whether this field is required to be filled out when bound.
- *
- * @param Boolean $required
- *
- * @return FormBuilder The current builder
- */
- public function setRequired($required)
- {
- $this->required = (Boolean) $required;
- return $this;
- }
- /**
- * Returns whether this field is required to be filled out when bound.
- *
- * @return Boolean Whether this field is required
- */
- public function getRequired()
- {
- return $this->required;
- }
- /**
- * Sets whether errors bubble up to the parent.
- *
- * @param type $errorBubbling
- *
- * @return FormBuilder The current builder
- */
- public function setErrorBubbling($errorBubbling)
- {
- $this->errorBubbling = (Boolean) $errorBubbling;
- return $this;
- }
- /**
- * Returns whether errors bubble up to the parent.
- *
- * @return Boolean
- */
- public function getErrorBubbling()
- {
- return $this->errorBubbling;
- }
- /**
- * Adds a validator to the form.
- *
- * @param FormValidatorInterface $validator The validator
- *
- * @return FormBuilder The current builder
- */
- public function addValidator(FormValidatorInterface $validator)
- {
- $this->validators[] = $validator;
- return $this;
- }
- /**
- * Returns the validators used by the form.
- *
- * @return array An array of FormValidatorInterface
- */
- public function getValidators()
- {
- return $this->validators;
- }
- /**
- * Adds an event listener for events on this field
- *
- * @see Symfony\Component\EventDispatcher\EventDispatcherInterface::addEventListener
- *
- * @return FormBuilder The current builder
- */
- 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
- *
- * @return FormBuilder The current builder
- */
- public function addEventSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
- {
- $this->dispatcher->addSubscriber($subscriber, $priority);
- return $this;
- }
- /**
- * Appends a transformer to the normalization transformer chain
- *
- * @param DataTransformerInterface $clientTransformer
- *
- * @return FormBuilder The current builder
- */
- public function appendNormTransformer(DataTransformerInterface $normTransformer)
- {
- $this->normTransformers[] = $normTransformer;
- return $this;
- }
- /**
- * Prepends a transformer to the client transformer chain
- *
- * @param DataTransformerInterface $normTransformer
- *
- * @return FormBuilder The current builder
- */
- public function prependNormTransformer(DataTransformerInterface $normTransformer)
- {
- array_unshift($this->normTransformers, $normTransformer);
- return $this;
- }
- /**
- * Clears the normalization transformers.
- *
- * @return FormBuilder The current builder
- */
- public function resetNormTransformers()
- {
- $this->normTransformers = array();
- return $this;
- }
- /**
- * Returns all the normalization transformers.
- *
- * @return array An array of DataTransformerInterface
- */
- public function getNormTransformers()
- {
- return $this->normTransformers;
- }
- /**
- * Appends a transformer to the client transformer chain
- *
- * @param DataTransformerInterface $clientTransformer
- *
- * @return FormBuilder The current builder
- */
- public function appendClientTransformer(DataTransformerInterface $clientTransformer)
- {
- $this->clientTransformers[] = $clientTransformer;
- return $this;
- }
- /**
- * Prepends a transformer to the client transformer chain
- *
- * @param DataTransformerInterface $clientTransformer
- *
- * @return FormBuilder The current builder
- */
- public function prependClientTransformer(DataTransformerInterface $clientTransformer)
- {
- array_unshift($this->clientTransformers, $clientTransformer);
- return $this;
- }
- public function resetClientTransformers()
- {
- $this->clientTransformers = array();
- }
- /**
- * Returns all the client transformers.
- *
- * @return array An array of DataTransformerInterface
- */
- public function getClientTransformers()
- {
- return $this->clientTransformers;
- }
- /**
- * Sets the value for an attribute.
- *
- * @param string $name The name of the attribute
- * @param string $value The value of the attribute
- *
- * @return FormBuilder The current builder
- */
- public function setAttribute($name, $value)
- {
- $this->attributes[$name] = $value;
- return $this;
- }
- /**
- * Returns the value of the attributes with the given name.
- *
- * @param string $name The name of the attribute
- */
- public function getAttribute($name)
- {
- return $this->attributes[$name];
- }
- /**
- * Returns whether the form has an attribute with the given name.
- *
- * @param string $name The name of the attribute
- */
- public function hasAttribute($name)
- {
- return isset($this->attributes[$name]);
- }
- /**
- * Returns all the attributes.
- *
- * @return array An array of attributes
- */
- public function getAttributes()
- {
- return $this->attributes;
- }
- /**
- * Sets the data mapper used by the form.
- *
- * @param DataMapperInterface $dataMapper
- *
- * @return FormBuilder The current builder
- */
- public function setDataMapper(DataMapperInterface $dataMapper)
- {
- $this->dataMapper = $dataMapper;
- return $this;
- }
- /**
- * Returns the data mapper used by the form.
- *
- * @return array An array of DataMapperInterface
- */
- public function getDataMapper()
- {
- return $this->dataMapper;
- }
- /**
- * Set the types.
- *
- * @param array $types An array FormTypeInterface
- *
- * @return FormBuilder The current builder
- */
- public function setTypes(array $types)
- {
- $this->types = $types;
- return $this;
- }
- /**
- * Return the types.
- *
- * @return array An array of FormTypeInterface
- */
- public function getTypes()
- {
- return $this->types;
- }
- /**
- * Sets the data used for the client data when no value is bound.
- *
- * @param mixed $emptyData
- */
- public function setEmptyData($emptyData)
- {
- $this->emptyData = $emptyData;
- return $this;
- }
- /**
- * Returns the data used for the client data when no value is bound.
- *
- * @return mixed
- */
- public function getEmptyData()
- {
- return $this->emptyData;
- }
- /**
- * 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>
- *
- * @param string|FormBuilder $child
- * @param string|FormTypeInterface $type
- * @param array $options
- *
- * @return FormBuilder The current builder
- */
- public function add($child, $type = null, array $options = array())
- {
- if ($child instanceof self) {
- $this->children[$child->getName()] = $child;
- return $this;
- }
- if (!is_string($child)) {
- throw new UnexpectedTypeException($child, 'string or Symfony\Component\Form\FormBuilder');
- }
- if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
- throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
- }
- $this->children[$child] = array(
- 'type' => $type,
- 'options' => $options,
- );
- return $this;
- }
- public function create($name, $type = null, array $options = array())
- {
- if (null !== $type) {
- $builder = $this->getFormFactory()->createNamedBuilder(
- $type,
- $name,
- null,
- $options
- );
- } else {
- if (!$this->dataClass) {
- throw new FormException('The data class must be set to automatically create children');
- }
- $builder = $this->getFormFactory()->createBuilderForProperty(
- $this->dataClass,
- $name,
- null,
- $options
- );
- }
- return $builder;
- }
- public function get($name)
- {
- if (!isset($this->children[$name])) {
- throw new FormException(sprintf('The field "%s" does not exist', $name));
- }
- if (!$this->children[$name] instanceof FormBuilder) {
- $this->children[$name] = $this->create(
- $name,
- $this->children[$name]['type'],
- $this->children[$name]['options']
- );
- }
- return $this->children[$name];
- }
- /**
- * Removes the field with the given name.
- *
- * @param string $name
- */
- public function remove($name)
- {
- if (isset($this->children[$name])) {
- unset($this->children[$name]);
- }
- }
- /**
- * Returns whether a field with the given name exists.
- *
- * @param string $name
- *
- * @return Boolean
- */
- public function has($name)
- {
- return isset($this->children[$name]);
- }
- /**
- * Creates the form.
- *
- * @return Form The form
- */
- public function getForm()
- {
- $instance = new Form(
- $this->getName(),
- $this->buildDispatcher(),
- $this->getTypes(),
- $this->getClientTransformers(),
- $this->getNormTransformers(),
- $this->getDataMapper(),
- $this->getValidators(),
- $this->getRequired(),
- $this->getReadOnly(),
- $this->getErrorBubbling(),
- $this->getEmptyData(),
- $this->getAttributes()
- );
- foreach ($this->buildChildren() as $child) {
- $instance->add($child);
- }
- if ($this->getData()) {
- $instance->setData($this->getData());
- }
- return $instance;
- }
- /**
- * Returns the event dispatcher.
- *
- * @return type
- */
- protected function buildDispatcher()
- {
- return $this->dispatcher;
- }
- /**
- * Creates the children.
- *
- * @return array An array of Form
- */
- protected function buildChildren()
- {
- $children = array();
- foreach ($this->children as $name => $builder) {
- if (!$builder instanceof FormBuilder) {
- $builder = $this->create($name, $builder['type'], $builder['options']);
- }
- $children[$builder->getName()] = $builder->getForm();
- }
- return $children;
- }
- }
|