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

Skip to content

Commit d5234b9

Browse files
committed
[Form] Add position support
1 parent d8f839d commit d5234b9

File tree

11 files changed

+828
-6
lines changed

11 files changed

+828
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<parameters>
8+
<parameter key="form.orderer.class">Symfony\Component\Form\FormOrderer</parameter>
89
<parameter key="form.resolved_type_factory.class">Symfony\Component\Form\ResolvedFormTypeFactory</parameter>
910
<parameter key="form.registry.class">Symfony\Component\Form\FormRegistry</parameter>
1011
<parameter key="form.factory.class">Symfony\Component\Form\FormFactory</parameter>
@@ -14,8 +15,13 @@
1415
</parameters>
1516

1617
<services>
18+
<!-- FormOrderer -->
19+
<service id="form.orderer" class="%form.orderer.class%" />
20+
1721
<!-- ResolvedFormTypeFactory -->
18-
<service id="form.resolved_type_factory" class="%form.resolved_type_factory.class%" />
22+
<service id="form.resolved_type_factory" class="%form.resolved_type_factory.class%">
23+
<argument type="service" id="form.orderer" />
24+
</service>
1925

2026
<!-- FormRegistry -->
2127
<service id="form.registry" class="%form.registry.class%">

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
*/
@@ -516,6 +522,28 @@ public function setAutoInitialize($initialize)
516522
return $this;
517523
}
518524

525+
/**
526+
* {@inheritdoc}
527+
*/
528+
public function setPosition($position)
529+
{
530+
if ($this->locked) {
531+
throw new BadMethodCallException('The config builder cannot be modified anymore.');
532+
}
533+
534+
if (is_string($position) && ($position !== 'first') && ($position !== 'last')) {
535+
throw new InvalidConfigurationException('If you use position as string, you can only use "first" & "last".');
536+
}
537+
538+
if (is_array($position) && !isset($position['before']) && !isset($position['after'])) {
539+
throw new InvalidConfigurationException('If you use position as array, you must at least define the "before" or "after" option.');
540+
}
541+
542+
$this->position = $position;
543+
544+
return $this;
545+
}
546+
519547
/**
520548
* Unsupported method.
521549
*
@@ -795,6 +823,14 @@ public function getAutoInitialize()
795823
return false;
796824
}
797825

826+
/**
827+
* {@inheritdoc}
828+
*/
829+
public function getPosition()
830+
{
831+
return $this->position;
832+
}
833+
798834
/**
799835
* Unsupported method.
800836
*

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
/**
@@ -121,6 +123,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
121123
'attr' => array(),
122124
'translation_domain' => null,
123125
'auto_initialize' => true,
126+
'position' => null,
124127
));
125128

126129
$resolver->setAllowedTypes(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
*/
@@ -528,6 +534,14 @@ public function getAutoInitialize()
528534
return $this->autoInitialize;
529535
}
530536

537+
/**
538+
* {@inheritdoc}
539+
*/
540+
public function getPosition()
541+
{
542+
return $this->position;
543+
}
544+
531545
/**
532546
* {@inheritdoc}
533547
*/
@@ -860,6 +874,28 @@ public function setAutoInitialize($initialize)
860874
return $this;
861875
}
862876

877+
/**
878+
* {@inheritdoc}
879+
*/
880+
public function setPosition($position)
881+
{
882+
if ($this->locked) {
883+
throw new BadMethodCallException('The config builder cannot be modified anymore.');
884+
}
885+
886+
if (is_string($position) && ($position !== 'first') && ($position !== 'last')) {
887+
throw new InvalidConfigurationException('If you use position as string, you can only use "first" & "last".');
888+
}
889+
890+
if (is_array($position) && !isset($position['before']) && !isset($position['after'])) {
891+
throw new InvalidConfigurationException('If you use position as array, you must at least define the "before" or "after" option.');
892+
}
893+
894+
$this->position = $position;
895+
896+
return $this;
897+
}
898+
863899
/**
864900
* {@inheritdoc}
865901
*/

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