Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function add(FormInterface $child)
 {
     if ($this->bound) {
         throw new AlreadyBoundException('You cannot add children to a bound form');
     }
     if (!$this->config->getCompound()) {
         throw new FormException('You cannot add children to a simple form. Maybe you should set the option "compound" to true?');
     }
     // Obtain the view data
     $viewData = null;
     // If setData() is currently being called, there is no need to call
     // mapDataToForms() here, as mapDataToForms() is called at the end
     // of setData() anyway. Not doing this check leads to an endless
     // recursion when initializing the form lazily and an event listener
     // (such as ResizeFormListener) adds fields depending on the data:
     //
     //  * setData() is called, the form is not initialized yet
     //  * add() is called by the listener (setData() is not complete, so
     //    the form is still not initialized)
     //  * getViewData() is called
     //  * setData() is called since the form is not initialized yet
     //  * ... endless recursion ...
     if (!$this->lockSetData) {
         $viewData = $this->getViewData();
     }
     $this->children[$child->getName()] = $child;
     $child->setParent($this);
     if (!$this->lockSetData) {
         $this->config->getDataMapper()->mapDataToForms($viewData, array($child));
     }
     return $this;
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function add($child, $type = null, array $options = array())
 {
     if ($this->submitted) {
         throw new AlreadySubmittedException('You cannot add children to a submitted form');
     }
     if (!$this->config->getCompound()) {
         throw new LogicException('You cannot add children to a simple form. Maybe you should set the option "compound" to true?');
     }
     // Obtain the view data
     $viewData = null;
     // If setData() is currently being called, there is no need to call
     // mapDataToForms() here, as mapDataToForms() is called at the end
     // of setData() anyway. Not doing this check leads to an endless
     // recursion when initializing the form lazily and an event listener
     // (such as ResizeFormListener) adds fields depending on the data:
     //
     //  * setData() is called, the form is not initialized yet
     //  * add() is called by the listener (setData() is not complete, so
     //    the form is still not initialized)
     //  * getViewData() is called
     //  * setData() is called since the form is not initialized yet
     //  * ... endless recursion ...
     //
     // Also skip data mapping if setData() has not been called yet.
     // setData() will be called upon form initialization and data mapping
     // will take place by then.
     if (!$this->lockSetData && $this->defaultDataSet && !$this->config->getInheritData()) {
         $viewData = $this->getViewData();
     }
     if (!$child instanceof FormInterface) {
         if (!is_string($child) && !is_int($child)) {
             throw new UnexpectedTypeException($child, 'string, integer or Symfony\\Component\\Form\\FormInterface');
         }
         if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
             throw new UnexpectedTypeException($type, 'string or Symfony\\Component\\Form\\FormTypeInterface');
         }
         // Never initialize child forms automatically
         $options['auto_initialize'] = false;
         if (null === $type && null === $this->config->getDataClass()) {
             $type = 'Symfony\\Component\\Form\\Extension\\Core\\Type\\TextType';
         }
         if (null === $type) {
             $child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
         } else {
             $child = $this->config->getFormFactory()->createNamed($child, $type, null, $options);
         }
     } elseif ($child->getConfig()->getAutoInitialize()) {
         throw new RuntimeException(sprintf('Automatic initialization is only supported on root forms. You ' . 'should set the "auto_initialize" option to false on the field "%s".', $child->getName()));
     }
     $this->children[$child->getName()] = $child;
     $child->setParent($this);
     if (!$this->lockSetData && $this->defaultDataSet && !$this->config->getInheritData()) {
         $iterator = new InheritDataAwareIterator(new \ArrayIterator(array($child->getName() => $child)));
         $iterator = new \RecursiveIteratorIterator($iterator);
         $this->config->getDataMapper()->mapDataToForms($viewData, $iterator);
     }
     return $this;
 }
Exemplo n.º 3
0
    /**
     * {@inheritdoc}
     */
    public function add($child, $type = null, array $options = array())
    {
        if ($this->bound) {
            throw new AlreadyBoundException('You cannot add children to a bound form');
        }

        if (!$this->config->getCompound()) {
            throw new Exception('You cannot add children to a simple form. Maybe you should set the option "compound" to true?');
        }

        // Obtain the view data
        $viewData = null;

        // If setData() is currently being called, there is no need to call
        // mapDataToForms() here, as mapDataToForms() is called at the end
        // of setData() anyway. Not doing this check leads to an endless
        // recursion when initializing the form lazily and an event listener
        // (such as ResizeFormListener) adds fields depending on the data:
        //
        //  * setData() is called, the form is not initialized yet
        //  * add() is called by the listener (setData() is not complete, so
        //    the form is still not initialized)
        //  * getViewData() is called
        //  * setData() is called since the form is not initialized yet
        //  * ... endless recursion ...
        if (!$this->lockSetData) {
            $viewData = $this->getViewData();
        }

        if (!$child instanceof FormInterface) {
            if (!is_string($child) && !is_int($child)) {
                throw new UnexpectedTypeException($child, 'string, integer or Symfony\Component\Form\FormInterface');
            }

            if (null !== $type && !is_string($type) && !$type instanceof FormTypeInterface) {
                throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
            }

            if (null === $type) {
                $child = $this->config->getFormFactory()->createForProperty($this->config->getDataClass(), $child, null, $options);
            } else {
                $child = $this->config->getFormFactory()->createNamed($child, $type, null, $options);
            }
        }

        $this->children[$child->getName()] = $child;

        $child->setParent($this);

        if (!$this->lockSetData) {
            $this->config->getDataMapper()->mapDataToForms($viewData, array($child));
        }

        return $this;
    }
Exemplo n.º 4
0
 public function __construct(FormConfigInterface $config)
 {
     if ($config->getCompound() && !$config->getDataMapper()) {
         throw new LogicException('Compound forms need a data mapper');
     }
     if ($config->getInheritData()) {
         $this->defaultDataSet = true;
     }
     $this->config = $config;
     $this->children = new OrderedHashMap();
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function add(FormInterface $child)
 {
     if ($this->bound) {
         throw new AlreadyBoundException('You cannot add children to a bound form');
     }
     $this->children[$child->getName()] = $child;
     $child->setParent($this);
     if ($this->config->getDataMapper()) {
         $this->config->getDataMapper()->mapDataToForms($this->getViewData(), array($child));
     }
     return $this;
 }
Exemplo n.º 6
0
 /**
  * {@inheritdoc}
  */
 public function add(FormInterface $child)
 {
     if ($this->bound) {
         throw new AlreadyBoundException('You cannot add children to a bound form');
     }
     if (!$this->config->getCompound()) {
         throw new FormException('You cannot add children to a simple form. Maybe you should set the option "compound" to true?');
     }
     $this->children[$child->getName()] = $child;
     $child->setParent($this);
     $this->config->getDataMapper()->mapDataToForms($this->getViewData(), array($child));
     return $this;
 }
Exemplo n.º 7
0
 /**
  * Creates an unmodifiable copy of a given configuration.
  *
  * @param  FormConfigInterface $config The configuration to copy.
  */
 public function __construct(FormConfigInterface $config)
 {
     $dispatcher = $config->getEventDispatcher();
     if (!$dispatcher instanceof UnmodifiableEventDispatcher) {
         $dispatcher = new UnmodifiableEventDispatcher($dispatcher);
     }
     $this->dispatcher = $dispatcher;
     $this->name = $config->getName();
     $this->propertyPath = $config->getPropertyPath();
     $this->mapped = $config->getMapped();
     $this->byReference = $config->getByReference();
     $this->virtual = $config->getVirtual();
     $this->compound = $config->getCompound();
     $this->types = $config->getTypes();
     $this->viewTransformers = $config->getViewTransformers();
     $this->modelTransformers = $config->getModelTransformers();
     $this->dataMapper = $config->getDataMapper();
     $this->validators = $config->getValidators();
     $this->required = $config->getRequired();
     $this->disabled = $config->getDisabled();
     $this->errorBubbling = $config->getErrorBubbling();
     $this->emptyData = $config->getEmptyData();
     $this->attributes = $config->getAttributes();
     $this->data = $config->getData();
     $this->dataClass = $config->getDataClass();
     $this->options = $config->getOptions();
 }