8000 [Form] Add position support · symfony/symfony@c929615 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit c929615

Browse files
committed
[Form] Add position support
1 parent afed2f8 commit c929615

File tree

8 files changed

+829
-5
lines changed

8 files changed

+829
-5
lines changed

src/Symfony/Component/Form/ButtonBuilder.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
namespace Symfony\Component\Form;
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15-
use Symfony\Component\Form\Exception\InvalidArgumentException;
1615
use Symfony\Component\Form\Exception\BadMethodCallException;
16+
use Symfony\Component\Form\Exception\InvalidArgumentException;
17+
use Symfony\Component\Form\Exception\InvalidConfigurationException;
1718

1819
/**
1920
* A builder for {@link Button} instances.
2021
*
2122
* @author Bernhard Schussek <bschussek@gmail.com>
2223
*/
23-
class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
24+
class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface, OrderedFormConfigBuilderInterface
2425
{
2526
/**
2627
* @var bool
@@ -47,6 +48,11 @@ class ButtonBuilder implements \IteratorAggregate, FormBuilderInterface
4748
*/
4849
private $attributes = array();
4950

51+
/**
52+
* @var null|string|array
53+
*/
54+
private $position;
55+
5056
/**
5157
* @var array
5258
*/
@@ -502,6 +508,28 @@ public function setAutoInitialize($initialize)
502508
return $this;
503509
}
504510

511+
/**
512+
* {@inheritdoc}
513+
*/
514+
public function setPosition($position)
515+
{
516+
if ($this->locked) {
517+
throw new BadMethodCallException('The config builder cannot be modified anymore.');
518+
}
519+
520+
if (is_string($position) && ($position !== 'first') && ($position !== 'last')) {
521+
throw new InvalidConfigurationException('If you use position as string, you can only use "first" & "last".');
522+
}
523+
524+
if (is_array($position) && !isset($position['before']) && !isset($position['after'])) {
525+
throw new InvalidConfigurationException('If you use position as array, you must at least define the "before" or "after" option.');
526+
}
527+
528+
$this->position = $position;
529+
530+
return $this;
531+
}
532+
505533
/**
506534
* Unsupported method.
507535
*
@@ -751,6 +779,14 @@ public function getAutoInitialize()
751779
return false;
752780
}
753781

782+
/**
783+
* {@inheritdoc}
784+
*/
785+
public function getPosition()
786+
{
787+
return $this->position;
788+
}
789+
754790
/**
755791
* Unsupported method.
756792
*

src/Symfony/Component/Form/Extension/Core/Type/BaseType.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ abstract class BaseType extends AbstractType
3232
*/
3333
public function buildForm(FormBuilderInterface $builder, array $options)
3434
{
35-
$builder->setDisabled($options['disabled']);
36-
$builder->setAutoInitialize($options['auto_initialize']);
35+
$builder
36+
->setDisabled($options['disabled'])
37+
->setAutoInitialize($options['auto_initialize'])
38+
->setPosition($options['position']);
3739
}
3840

3941
/**
@@ -117,6 +119,7 @@ public function configureOptions(OptionsResolver $resolver)
117119
'attr' => array(),
118120
'translation_domain' => null,
119121
'auto_initialize' => true,
122+
'position' => null,
120123
));
121124

122125
$resolver->setAllowedTypes('attr', 'array');

src/Symfony/Component/Form/FormConfigBuilder.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\Exception\BadMethodCallException;
1515
use Symfony\Component\Form\Exception\InvalidArgumentException;
16+
use Symfony\Component\Form\Exception\InvalidConfigurationException;
1617
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1718
use Symfony\Component\PropertyAccess\PropertyPath;
1819
use Symfony\Component\PropertyAccess\PropertyPathInterface;
@@ -25,7 +26,7 @@
2526
*
2627
* @author Bernhard Schussek <bschussek@gmail.com>
2728
*/
28-
class FormConfigBuilder implements FormConfigBuilderInterface
29+
class FormConfigBuilder implements FormConfigBuilderInterface, OrderedFormConfigBuilderInterface
2930
{
3031
/**
3132
* Caches a globally unique {@link NativeRequestHandler} instance.
@@ -172,6 +173,11 @@ class FormConfigBuilder implements FormConfigBuilderInterface
172173
*/
173174
private $autoInitialize = false;
174175

176+
/**
177+
* @var null|string|array
178+
*/
179+
private $position;
180+
175181
/**
176182
* @var array
177183
*/
@@ -513,6 +519,14 @@ public function getAutoInitialize()
513519
return $this->autoInitialize;
514520
}
515521

522+
/**
523+
* {@inheritdoc}
524+
*/
525+
public function getPosition()
526+
{
527+
return $this->position;
528+
}
529+
516530
/**
517531
* {@inheritdoc}
518532
*/
@@ -831,6 +845,28 @@ public function setAutoInitialize($initialize)
831845
return $this;
832846
}
833847

848+
/**
849+
* {@inheritdoc}
850+
*/
851+
public function setPosition($position)
852+
{
853+
if ($this->locked) {
854+
throw new BadMethodCallException('The config builder cannot be modified anymore.');
855+
}
856+
857+
if (is_string($position) && ($position !== 'first') && ($position !== 'last')) {
858+
throw new InvalidConfigurationException('If you use position as string, you can only use "first" & "last".');
859+
}
860+
861+
if (is_array($position) && !isset($position['before']) && !isset($position['after'])) {
862+
throw new InvalidConfigurationException('If you use position as array, you must at least define the "before" or "after" option.');
863+
}
864+
865+
$this->position = $position;
866+
867+
return $this;
868+
}
869+
834870
/**
835871
* {@inheritdoc}
836872
*/

0 commit comments

Comments
 (0)
0