2012-12-31 14:29:36 +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;
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
use Symfony\Component\Form\Exception\AlreadySubmittedException;
|
2012-12-30 15:38:36 +00:00
|
|
|
use Symfony\Component\Form\Exception\BadMethodCallException;
|
2012-12-31 14:29:36 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-11 15:58:10 +01:00
|
|
|
* A form button.
|
2012-12-31 14:29:36 +00:00
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bschussek@gmail.com>
|
|
|
|
*/
|
|
|
|
class Button implements \IteratorAggregate, FormInterface
|
|
|
|
{
|
|
|
|
/**
|
2019-03-12 20:16:42 +00:00
|
|
|
* @var FormInterface
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
private $parent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FormConfigInterface
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
2014-04-16 11:30:19 +01:00
|
|
|
* @var bool
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
private $submitted = false;
|
2012-12-31 14:29:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new button from a form configuration.
|
|
|
|
*/
|
|
|
|
public function __construct(FormConfigInterface $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* @param mixed $offset
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return bool Always returns false
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function offsetExists($offset)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* This method should not be invoked.
|
|
|
|
*
|
|
|
|
* @param mixed $offset
|
|
|
|
*
|
2013-04-15 21:11:02 +01:00
|
|
|
* @throws BadMethodCallException
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function offsetGet($offset)
|
|
|
|
{
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot have children.');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* This method should not be invoked.
|
|
|
|
*
|
|
|
|
* @param mixed $offset
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
2013-04-15 21:11:02 +01:00
|
|
|
* @throws BadMethodCallException
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value)
|
|
|
|
{
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot have children.');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* This method should not be invoked.
|
|
|
|
*
|
|
|
|
* @param mixed $offset
|
|
|
|
*
|
2013-04-15 21:11:02 +01:00
|
|
|
* @throws BadMethodCallException
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function offsetUnset($offset)
|
|
|
|
{
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot have children.');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setParent(FormInterface $parent = null)
|
|
|
|
{
|
2018-01-25 01:37:16 +00:00
|
|
|
if ($this->submitted) {
|
|
|
|
throw new AlreadySubmittedException('You cannot set the parent of a submitted button');
|
|
|
|
}
|
|
|
|
|
2012-12-31 14:29:36 +00:00
|
|
|
$this->parent = $parent;
|
2019-03-12 20:16:42 +00:00
|
|
|
|
|
|
|
return $this;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getParent()
|
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* This method should not be invoked.
|
|
|
|
*
|
2013-04-15 21:11:02 +01:00
|
|
|
* @throws BadMethodCallException
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
2019-01-16 09:39:14 +00:00
|
|
|
public function add($child, $type = null, array $options = [])
|
2012-12-31 14:29:36 +00:00
|
|
|
{
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot have children.');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* This method should not be invoked.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
2013-04-15 21:11:02 +01:00
|
|
|
* @throws BadMethodCallException
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function get($name)
|
|
|
|
{
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot have children.');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return bool Always returns false
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function has($name)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* This method should not be invoked.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
2013-04-15 21:11:02 +01:00
|
|
|
* @throws BadMethodCallException
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function remove($name)
|
|
|
|
{
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot have children.');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function all()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return [];
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-01-10 12:53:52 +00:00
|
|
|
public function getErrors($deep = false, $flatten = true)
|
2012-12-31 14:29:36 +00:00
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return new FormErrorIterator($this, []);
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* This method should not be invoked.
|
|
|
|
*
|
2015-03-20 13:26:52 +00:00
|
|
|
* @param mixed $modelData
|
2019-03-12 20:16:42 +00:00
|
|
|
*
|
|
|
|
* @return $this
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function setData($modelData)
|
|
|
|
{
|
2019-03-12 20:16:42 +00:00
|
|
|
// no-op, called during initialization of the form tree
|
|
|
|
return $this;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*/
|
|
|
|
public function getData()
|
|
|
|
{
|
2019-03-12 20:16:42 +00:00
|
|
|
return null;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*/
|
|
|
|
public function getNormData()
|
|
|
|
{
|
2019-03-12 20:16:42 +00:00
|
|
|
return null;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*/
|
|
|
|
public function getViewData()
|
|
|
|
{
|
2019-03-12 20:16:42 +00:00
|
|
|
return null;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return array Always returns an empty array
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function getExtraData()
|
|
|
|
{
|
2019-01-16 09:39:14 +00:00
|
|
|
return [];
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the button's configuration.
|
|
|
|
*
|
2019-03-12 20:16:42 +00:00
|
|
|
* @return FormConfigInterface The configuration instance
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the button is submitted.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return bool true if the button was submitted
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
2013-04-20 16:32:55 +01:00
|
|
|
public function isSubmitted()
|
2012-12-31 14:29:36 +00:00
|
|
|
{
|
2013-04-20 16:32:55 +01:00
|
|
|
return $this->submitted;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name by which the button is identified in forms.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return string The name of the button
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->config->getName();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*/
|
|
|
|
public function getPropertyPath()
|
|
|
|
{
|
2019-03-12 20:16:42 +00:00
|
|
|
return null;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2013-04-15 21:11:02 +01:00
|
|
|
* @throws BadMethodCallException
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function addError(FormError $error)
|
|
|
|
{
|
2013-04-15 21:11:02 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot have errors.');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return bool Always returns true
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function isValid()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return bool Always returns false
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function isRequired()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function isDisabled()
|
|
|
|
{
|
2019-03-12 20:16:42 +00:00
|
|
|
if ($this->parent && $this->parent->isDisabled()) {
|
|
|
|
return true;
|
2014-01-22 15:19:57 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:16:42 +00:00
|
|
|
return $this->config->getDisabled();
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return bool Always returns true
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function isEmpty()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return bool Always returns true
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function isSynchronized()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-26 12:06:33 +01:00
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*/
|
|
|
|
public function getTransformationFailure()
|
|
|
|
{
|
2019-03-12 20:16:42 +00:00
|
|
|
return null;
|
2014-09-26 12:06:33 +01:00
|
|
|
}
|
|
|
|
|
2013-04-29 16:31:30 +01:00
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* @throws BadMethodCallException
|
|
|
|
*/
|
|
|
|
public function initialize()
|
|
|
|
{
|
2013-05-02 08:53:57 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot be initialized. Call initialize() on the root form instead.');
|
2013-04-29 16:31:30 +01:00
|
|
|
}
|
|
|
|
|
2012-12-30 15:38:36 +00:00
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
|
|
|
* @param mixed $request
|
|
|
|
*
|
|
|
|
* @throws BadMethodCallException
|
|
|
|
*/
|
2013-04-20 14:56:42 +01:00
|
|
|
public function handleRequest($request = null)
|
2012-12-30 15:38:36 +00:00
|
|
|
{
|
2013-04-29 16:31:30 +01:00
|
|
|
throw new BadMethodCallException('Buttons cannot handle requests. Call handleRequest() on the root form instead.');
|
2012-12-30 15:38:36 +00:00
|
|
|
}
|
|
|
|
|
2012-12-31 14:29:36 +00:00
|
|
|
/**
|
2013-04-20 16:32:55 +01:00
|
|
|
* Submits data to the button.
|
2012-12-31 14:29:36 +00:00
|
|
|
*
|
2019-03-12 20:16:42 +00:00
|
|
|
* @param string|null $submittedData Not used
|
2016-06-28 06:50:50 +01:00
|
|
|
* @param bool $clearMissing Not used
|
2012-12-31 14:29:36 +00:00
|
|
|
*
|
2016-12-26 07:50:27 +00:00
|
|
|
* @return $this
|
2012-12-31 14:29:36 +00:00
|
|
|
*
|
2017-09-11 10:28:55 +01:00
|
|
|
* @throws Exception\AlreadySubmittedException if the button has already been submitted
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
2013-04-25 15:08:09 +01:00
|
|
|
public function submit($submittedData, $clearMissing = true)
|
2012-12-31 14:29:36 +00:00
|
|
|
{
|
2013-04-20 16:32:55 +01:00
|
|
|
if ($this->submitted) {
|
|
|
|
throw new AlreadySubmittedException('A form can only be submitted once');
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
2013-04-20 16:32:55 +01:00
|
|
|
$this->submitted = true;
|
2012-12-31 14:29:36 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getRoot()
|
|
|
|
{
|
|
|
|
return $this->parent ? $this->parent->getRoot() : $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function isRoot()
|
|
|
|
{
|
|
|
|
return null === $this->parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function createView(FormView $parent = null)
|
|
|
|
{
|
|
|
|
if (null === $parent && $this->parent) {
|
|
|
|
$parent = $this->parent->createView();
|
|
|
|
}
|
|
|
|
|
2013-09-17 10:39:17 +01:00
|
|
|
$type = $this->config->getType();
|
|
|
|
$options = $this->config->getOptions();
|
|
|
|
|
|
|
|
$view = $type->createView($this, $parent);
|
|
|
|
|
|
|
|
$type->buildView($view, $this, $options);
|
|
|
|
$type->finishView($view, $this, $options);
|
|
|
|
|
|
|
|
return $view;
|
2012-12-31 14:29:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return int Always returns 0
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsupported method.
|
|
|
|
*
|
2016-06-28 06:50:50 +01:00
|
|
|
* @return \EmptyIterator Always returns an empty iterator
|
2012-12-31 14:29:36 +00:00
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new \EmptyIterator();
|
|
|
|
}
|
|
|
|
}
|