Skip to content

[Form] Add position support #11241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/Symfony/Component/Form/ButtonBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
namespace Symfony\Component\Form;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\InvalidConfigurationException;

/**
* A builder for {@link Button} instances.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface, OrderedFormConfigBuilderInterface
{
/**
* @var bool
Expand All @@ -47,6 +48,11 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
*/
private $attributes = array();

/**
* @var null|string|array
*/
private $position;

/**
* @var array
*/
Expand All @@ -58,7 +64,7 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
* @param string $name The name of the button
* @param array $options The button's options
*
* @throws InvalidArgumentException If the name is empty.
* @throws InvalidArgumentException if the name is empty
*/
public function __construct($name, array $options = array())
{
Expand Down Expand Up @@ -503,6 +509,28 @@ public function setAutoInitialize($initialize)
return $this;
}

/**
* {@inheritdoc}
*/
public function setPosition($position)
{
if ($this->locked) {
throw new BadMethodCallException('ButtonBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}

if (is_string($position) && $position !== 'first' && $position !== 'last') {
throw new InvalidConfigurationException('When using position as a string, the only supported values are "first" and "last".');
}

if (is_array($position) && !isset($position['before']) && !isset($position['after'])) {
throw new InvalidConfigurationException('When using position as an array, the "before" or "after" option must be defined.');
}

$this->position = $position;

return $this;
}

/**
* Unsupported method.
*
Expand Down Expand Up @@ -752,6 +780,14 @@ public function getAutoInitialize()
return false;
}

/**
* {@inheritdoc}
*/
public function getPosition()
{
return $this->position;
}

/**
* Unsupported method.
*
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ abstract class BaseType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->setDisabled($options['disabled']);
$builder->setAutoInitialize($options['auto_initialize']);
$builder
->setDisabled($options['disabled'])
->setAutoInitialize($options['auto_initialize'])
->setPosition($options['position']);
}

/**
Expand Down Expand Up @@ -117,6 +119,7 @@ public function configureOptions(OptionsResolver $resolver)
'attr' => array(),
'translation_domain' => null,
'auto_initialize' => true,
'position' => null,
));

$resolver->setAllowedTypes('attr', 'array');
Expand Down
46 changes: 41 additions & 5 deletions src/Symfony/Component/Form/FormConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\PropertyAccess\PropertyPathInterface;
Expand All @@ -25,7 +26,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormConfigBuilder implements FormConfigBuilderInterface
class FormConfigBuilder implements FormConfigBuilderInterface, OrderedFormConfigBuilderInterface
{
/**
* Caches a globally unique {@link NativeRequestHandler} instance.
Expand Down Expand Up @@ -172,6 +173,11 @@ class FormConfigBuilder implements FormConfigBuilderInterface
*/
private $autoInitialize = false;

/**
* @var null|string|array
*/
private $position;

/**
* @var array
*/
Expand All @@ -185,8 +191,8 @@ class FormConfigBuilder implements FormConfigBuilderInterface
* @param EventDispatcherInterface $dispatcher The event dispatcher
* @param array $options The form options
*
* @throws InvalidArgumentException If the data class is not a valid class or if
* the name contains invalid characters.
* @throws InvalidArgumentException if the data class is not a valid class or if
* the name contains invalid characters
*/
public function __construct($name, $dataClass, EventDispatcherInterface $dispatcher, array $options = array())
{
Expand Down Expand Up @@ -513,6 +519,14 @@ public function getAutoInitialize()
return $this->autoInitialize;
}

/**
* {@inheritdoc}
*/
public function getPosition()
{
return $this->position;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -831,6 +845,28 @@ public function setAutoInitialize($initialize)
return $this;
}

/**
* {@inheritdoc}
*/
public function setPosition($position)
{
if ($this->locked) {
throw new BadMethodCallException('FormConfigBuilder methods cannot be accessed anymore once the builder is turned into a FormConfigInterface instance.');
}

if (is_string($position) && ($position !== 'first') && ($position !== 'last')) {
throw new InvalidConfigurationException('When using position as a string, the only supported values are "first" and "last".');
}

if (is_array($position) && !isset($position['before']) && !isset($position['after'])) {
throw new InvalidConfigurationException('When using position as an array, the "before" or "after" option must be defined.');
}

$this->position = $position;

return $this;
}

/**
* {@inheritdoc}
*/
Expand All @@ -852,8 +888,8 @@ public function getFormConfig()
*
* @param string|int $name The tested form name
*
* @throws UnexpectedTypeException If the name is not a string or an integer.
* @throws InvalidArgumentException If the name contains invalid characters.
* @throws UnexpectedTypeException if the name is not a string or an integer
* @throws InvalidArgumentException if the name contains invalid characters
*/
public static function validateName($name)
{
Expand Down
Loading