10000 [Form] Add position support · symfony/symfony@4b47fcd · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b47fcd

Browse files
committed
[Form] Add position support
1 parent 48d91a8 commit 4b47fcd

File tree

8 files changed

+791
-3
lines changed

8 files changed

+791
-3
lines changed

src/Symfony/Component/Form/ButtonBuilder.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
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.
@@ -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+
*/
< 8000 /td>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: 36 additions & 0 deletions
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;
@@ -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
*/

src/Symfony/Component/Form/FormConfigBuilderInterface.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,40 @@ public function setRequestHandler(RequestHandlerInterface $requestHandler);
279279
*/
280280
public function setAutoInitialize($initialize);
281281

282+
/**
283+
* Sets the form position.
284+
*
285+
* * The position can be `null` to reflect the original forms order.
286+
*
287+
* * The position can be `first` to place this form at the first position.
288+
* If many forms are defined as `first`, the original order between these forms is maintained.
289+
* Warning, `first` does not mean "very first" if there are many forms which are defined as `first`
290+
* or if you set up an other form `before` this form.
291+
*
292+
* * The position can be `last` to place this form at the last position.
293+
* If many forms are defined as `last`, the original order between these forms is maintained.
294+
* Warning, `last` does not mean "very last" if there are many forms which are defined as `last`
295+
* or if you set up an other form `after` this form.
296+
*
297+
* * The position can be `array('before' => 'form_name')` to place this form before the `form_name` form.
298+
* If many forms defines the same `before` form, the original order between these forms is maintained.
299+
* Warning, `before` does not mean "just before" if there are many forms which defined the same `before` form.
300+
*
301+
* * The position can be `array('after' => 'form_name')` to place this form after the `form_name` form.
302+
* If many forms defines the same after form, the original order between these forms is maintained.
303+
* Warning, `after` does not mean "just after" if there are many forms which defined the same `after` form.
304+
*
305+
* You can combine the `after` & `before` options together or with `first` and/or `last` to achieve
306+
* more complex use cases.
307+
*
308+
* @param null|string|array $position The form position.
309+
*
310+
* @throws \Symfony\Component\Form\Exception\InvalidConfigurationException If the position is not valid.
311+
*
312+
* @return self The configuration object.
313+
*/
314+
public function setPosition($position);
315+
282316
/**
283317
* Builds and returns the form configuration.
284318
*

src/Symfony/Component/Form/FormConfigInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,15 @@ public function getRequestHandler();
218218
*/
219219
public function getAutoInitialize();
220220

221+
/**
222+
* Gets the form position.
223+
*
224+
* @see FormConfigBuilderInterface::setPosition
225+
*
226+
* @return null|string|array The position.
227+
*/
228+
public function getPosition();
229+
221230
/**
222231
* Returns all options passed during the construction of the form.
223232
*

0 commit comments

Comments
 (0)
0