This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Form/Button.php

446 lines
8.6 KiB
PHP
Raw Normal View History

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;
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
{
/**
2013-10-31 14:11:32 +00:00
* @var FormInterface|null
2012-12-31 14:29:36 +00:00
*/
private $parent;
/**
* @var FormConfigInterface
*/
private $config;
/**
* @var Boolean
*/
private $submitted = false;
2012-12-31 14:29:36 +00:00
/**
* Creates a new button from a form configuration.
*
* @param FormConfigInterface $config The button's configuration.
*/
public function __construct(FormConfigInterface $config)
{
$this->config = $config;
}
/**
* Unsupported method.
*
* @param mixed $offset
*
* @return Boolean Always returns false.
*/
public function offsetExists($offset)
{
return false;
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param mixed $offset
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function offsetGet($offset)
{
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
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function offsetSet($offset, $value)
{
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
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function offsetUnset($offset)
{
throw new BadMethodCallException('Buttons cannot have children.');
2012-12-31 14:29:36 +00:00
}
/**
* {@inheritdoc}
*/
public function setParent(FormInterface $parent = null)
{
$this->parent = $parent;
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return $this->parent;
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param int|string|FormInterface $child
* @param null $type
* @param array $options
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function add($child, $type = null, array $options = array())
{
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
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function get($name)
{
throw new BadMethodCallException('Buttons cannot have children.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* @param string $name
*
* @return Boolean Always returns false.
*/
public function has($name)
{
return false;
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param string $name
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function remove($name)
{
throw new BadMethodCallException('Buttons cannot have children.');
2012-12-31 14:29:36 +00:00
}
/**
* {@inheritdoc}
*/
public function all()
{
return array();
}
/**
* {@inheritdoc}
*/
public function getErrors()
{
return array();
}
/**
* Unsupported method.
*
* This method should not be invoked.
*
* @param string $modelData
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function setData($modelData)
{
// called during initialization of the form tree
// noop
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getData()
{
return null;
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getNormData()
{
return null;
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getViewData()
{
return null;
}
/**
* Unsupported method.
*
* @return array Always returns an empty array.
*/
public function getExtraData()
{
return array();
}
/**
* Returns the button's configuration.
*
* @return FormConfigInterface The configuration.
*/
public function getConfig()
{
return $this->config;
}
/**
* Returns whether the button is submitted.
*
* @return Boolean true if the button was submitted.
*/
public function isSubmitted()
2012-12-31 14:29:36 +00:00
{
return $this->submitted;
2012-12-31 14:29:36 +00:00
}
/**
* Returns the name by which the button is identified in forms.
*
* @return string The name of the button.
*/
public function getName()
{
return $this->config->getName();
}
/**
* Unsupported method.
*
* @return null Always returns null.
*/
public function getPropertyPath()
{
return null;
}
/**
* Unsupported method.
*
* @param FormError $error
*
* @throws BadMethodCallException
2012-12-31 14:29:36 +00:00
*/
public function addError(FormError $error)
{
throw new BadMethodCallException('Buttons cannot have errors.');
2012-12-31 14:29:36 +00:00
}
/**
* Unsupported method.
*
* @return Boolean Always returns true.
*/
public function isValid()
{
return true;
}
/**
* Unsupported method.
*
* @return Boolean Always returns false.
*/
public function isRequired()
{
return false;
}
/**
* {@inheritdoc}
*/
public function isDisabled()
{
return $this->config->getDisabled();
}
/**
* Unsupported method.
*
* @return Boolean Always returns true.
*/
public function isEmpty()
{
return true;
}
/**
* Unsupported method.
*
* @return Boolean Always returns true.
*/
public function isSynchronized()
{
return true;
}
/**
* 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.');
}
2012-12-30 15:38:36 +00:00
/**
* Unsupported method.
*
* @param mixed $request
*
* @throws BadMethodCallException
*/
public function handleRequest($request = null)
2012-12-30 15:38:36 +00: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
/**
* Submits data to the button.
2012-12-31 14:29:36 +00:00
*
* @param null|string $submittedData The data.
* @param bool $clearMissing Not used.
2012-12-31 14:29:36 +00:00
*
* @return Button The button instance
*
* @throws Exception\AlreadySubmittedException If the button has already been submitted.
2012-12-31 14:29:36 +00:00
*/
public function submit($submittedData, $clearMissing = true)
2012-12-31 14:29:36 +00:00
{
if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once');
2012-12-31 14:29:36 +00: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();
}
$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.
*
* @return integer Always returns 0.
*/
public function count()
{
return 0;
}
/**
* Unsupported method.
*
* @return \EmptyIterator Always returns an empty iterator.
*/
public function getIterator()
{
return new \EmptyIterator();
}
}