10000 [Form] Added FormBuilderInterface and FormViewInterface and cleaned u… · symfony/symfony@2cd99e8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2cd99e8

Browse files
committed
[Form] Added FormBuilderInterface and FormViewInterface and cleaned up FormTypeInterface and FormTypeExtensionInterface
1 parent 0ef4066 commit 2cd99e8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+763
-437
lines changed

UPGRADE-2.1.md

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -290,29 +290,6 @@
290290
* `FormUtil::toArrayKey()` and `FormUtil::toArrayKeys()` have been removed.
291291
They were merged into ChoiceList and have no public equivalent anymore.
292292
293-
* The options passed to the `getParent()` method of form types no longer
294-
contain default options. They only contain the options passed by the user.
295-
296-
You should check if options exist before attempting to read their value.
297-
298-
Before:
299-
300-
```
301-
public function getParent(array $options)
302-
{
303-
return 'single_text' === $options['widget'] ? 'text' : 'choice';
304-
}
305-
```
306-
307-
After:
308-
309-
```
310-
public function getParent(array $options)
311-
{
312-
return isset($options['widget']) && 'single_text' === $options['widget'] ? 'text' : 'choice';
313-
}
314-
```
315-
316293
* The `add()`, `remove()`, `setParent()`, `bind()` and `setData()` methods in
317294
the Form class now throw an exception if the form is already bound.
318295
@@ -611,22 +588,60 @@
611588
The second argument `$value` contains the current default value and
612589
does not have to be specified if not needed.
613590
591+
* No options are passed to `getParent()` of `FormTypeInterface` anymore. If
592+
you previously dynamically inherited from FormType or FieldType, you can now
593+
dynamically set the "single_control" option instead.
594+
595+
Before:
596+
597+
```
598+
public function getParent(array $options)
599+
{
600+
return $options['expanded'] ? 'form' : 'field';
601+
}
602+
```
603+
604+
After:
605+
606+
```
607+
public function setDefaultOptions(OptionsResolver $resolver)
608+
{
609+
$singleControl = function (Options $options) {
610+
return !$options['expanded'];
611+
};
612+
613+
$resolver->setDefaults(array(
614+
'single_control' => $singleControl,
615+
));
616+
}
617+
618+
public function getParent()
619+
{
620+
return 'form';
621+
}
622+
```
623+
614624
* A third argument $options was added to the methods `buildView()` and
615625
`buildViewBottomUp()` in `FormTypeInterface` and `FormTypeExtensionInterface`.
616-
You should adapt your implementing classes.
626+
Furthermore, `buildViewBottomUp()` was renamed to `finishView()`. At last,
627+
all methods in these types now receive instances of `FormBuilderInterface`
628+
and `FormViewInterface` where they received instances of `FormBuilder` and
629+
`FormView` before. You need to adapt your implementing classes.
617630
618631
Before:
619632
620633
```
634+
public function buildForm(FormBuilder $builder, array $options)
621635
public function buildView(FormView $view, FormInterface $form)
622636
public function buildViewBottomUp(FormView $view, FormInterface $form)
623637
```
624638
625639
After:
626640
627641
```
628-
public function buildView(FormView $view, FormInterface $form, array $options)
629-
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
642+
public function buildForm(FormBuilderInterface $builder, array $options)
643+
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
644+
public function buildViewBottomUp(FormViewInterface $view, FormInterface $form, array $options)
630645
```
631646
632647
### Validator

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Doctrine\Common\Persistence\ManagerRegistry;
1515
use Doctrine\Common\Persistence\ObjectManager;
16-
use Symfony\Component\Form\FormBuilder;
16+
use Symfony\Component\Form\FormBuilderInterface;
1717
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
1818
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
1919
use Symfony\Bridge\Doctrine\Form\EventListener\MergeDoctrineCollectionListener;
@@ -34,7 +34,7 @@ public function __construct(ManagerRegistry $registry)
3434
$this->registry = $registry;
3535
}
3636

37-
public function buildForm(FormBuilder $builder, array $options)
37+
public function buildForm(FormBuilderInterface $builder, array $options)
3838
{
3939
if ($options['multiple']) {
4040
$builder
@@ -94,7 +94,7 @@ public function setDefaultOptions(OptionsResolver $resolver)
9494
*/
9595
abstract public function getLoader(ObjectManager $manager, $queryBuilder, $class);
9696

97-
public function getParent(array $options)
97+
public function getParent()
9898
{
9999
return 'choice';
100100
}

src/Symfony/Bridge/Propel1/Form/Type/ModelType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Bridge\Propel1\Form\ChoiceList\ModelChoiceList;
1515
use Symfony\Bridge\Propel1\Form\DataTransformer\CollectionToArrayTransformer;
1616
use Symfony\Component\Form\AbstractType;
17-
use Symfony\Component\Form\FormBuilder;
17+
use Symfony\Component\Form\FormBuilderInterface;
1818
use Symfony\Component\OptionsResolver\Options;
1919
use Symfony\Component\OptionsResolver\OptionsResolver;
2020

@@ -25,7 +25,7 @@
2525
*/
2626
class ModelType extends AbstractType
2727
{
28-
public function buildForm(FormBuilder $builder, array $options)
28+
public function buildForm(FormBuilderInterface $builder, array $options)
2929
{
3030
if ($options['multiple']) {
3131
$builder->prependClientTransformer(new CollectionToArrayTransformer());
@@ -58,7 +58,7 @@ public function setDefaultOptions(OptionsResolver $resolver)
5858
));
5959
}
6060

61-
public function getParent(array $options)
61+
public function getParent()
6262
{
6363
return 'choice';
6464
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/Bundle/CsrfFormLoginBundle/Form/UserLoginFormType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\CsrfFormLoginBundle\Form;
1313

1414
use Symfony\Component\Form\AbstractType;
15-
use Symfony\Component\Form\FormBuilder;
15+
use Symfony\Component\Form\FormBuilderInterface;
1616
use Symfony\Component\Form\FormError;
1717
use Symfony\Component\Form\FormEvents;
1818
use Symfony\Component\Form\Event\FilterDataEvent;
@@ -29,7 +29,7 @@
2929
*/
3030
class UserLoginFormType extends AbstractType
3131
{
32-
private $reqeust;
32+
private $request;
3333

3434
/**
3535
* @param Request $request A request instance
@@ -42,7 +42,7 @@ public function __construct(Request $request)
4242
/**
4343
* @see Symfony\Component\Form\AbstractType::buildForm()
4444
*/
45-
public function buildForm(FormBuilder $builder, array $options)
45+
public function buildForm(FormBuilderInterface $builder, array $options)
4646
{
4747
$builder
4848
->add('username', 'text')

src/Symfony/Component/Form/AbstractType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,21 @@ abstract class AbstractType implements FormTypeInterface
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function buildForm(FormBuilder $builder, array $options)
31+
public function buildForm(FormBuilderInterface $builder, array $options)
3232
{
3333
}
3434

3535
/**
3636
* {@inheritdoc}
3737
*/
38-
public function buildView(FormView $view, FormInterface $form, array $options)
38+
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
3939
{
4040
}
4141

4242
/**
4343
* {@inheritdoc}
4444
*/
45-
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
45+
public function finishView(FormViewInterface $view, FormInterface $form, array $options)
4646
{
4747
}
4848

@@ -92,7 +92,7 @@ public function getAllowedOptionValues()
9292
/**
9393
* {@inheritdoc}
9494
*/
95-
public function getParent(array $options)
95+
public function getParent()
9696
{
9797
return 'form';
9898
}

src/Symfony/Component/Form/AbstractTypeExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ abstract class AbstractTypeExtension implements FormTypeExtensionInterface
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
public function buildForm(FormBuilder $builder, array $options)
24+
public function buildForm(FormBuilderInterface $builder, array $options)
2525
{
2626
}
2727

2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function buildView(FormView $view, FormInterface $form, array $options)
31+
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
3232
{
3333
}
3434

3535
/**
3636
* {@inheritdoc}
3737
*/
38-
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
38+
public function finishView(FormViewInterface $view, FormInterface $form, array $options)
3939
{
4040
}
4141

src/Symfony/Component/Form/CHANGELOG.md 10000

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ CHANGELOG
4343
* forms now don't create an empty object anymore if they are completely
4444
empty and not required. The empty value for such forms is null.
4545
* added constant Guess::VERY_HIGH_CONFIDENCE
46-
* [BC BREAK] FormType::getParent() does not see default options anymore
4746
* [BC BREAK] The methods `add`, `remove`, `setParent`, `bind` and `setData`
4847
in class Form now throw an exception if the form is already bound
4948
* fields of constrained classes without a NotBlank or NotNull constraint are
@@ -95,8 +94,15 @@ CHANGELOG
9594
* deprecated the methods `getDefaultOptions` and `getAllowedOptionValues`
9695
in FormTypeInterface and FormTypeExtensionInterface
9796
* options passed during construction can now be accessed from FormConfigInterface
97+
* added FormBuilderInterface, FormViewInterface and FormConfigEditorInterface
98+
* [BC BREAK] the methods in FormTypeInterface and FormTypeExtensionInterface now
99+
receive FormBuilderInterface and FormViewInterface instead of FormBuilder and
100+
FormView
101+
* [BC BREAK] the method `buildViewBottomUp` was renamed to `finishView` in
102+
FormTypeInterface and FormTypeExtensionInterface
98103
* [BC BREAK] the options array is now passed as last argument of the
99104
methods
100105
* `buildView`
101-
* `buildViewBottomUp`
106+
* `finishView`
102107
in FormTypeInterface and FormTypeExtensionInterface
108+
* [BC BREAK] no options are passed to `getParent` of FormTypeInterface anymore

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function setDefaultOptions(OptionsResolver $resolver)
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
public function getParent(array $options)
32+
public function getParent()
3333
{
3434
return 'date';
3535
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15-
use Symfony\Component\Form\FormBuilder;
15+
use Symfony\Component\Form\FormBuilderInterface;
1616
use Symfony\Component\Form\FormInterface;
1717
use Symfony\Component\Form\Extension\Core\DataTransformer\BooleanToStringTransformer;
18-
use Symfony\Component\Form\FormView;
18+
use Symfony\Component\Form\FormViewInterface;
1919
use Symfony\Component\OptionsResolver\OptionsResolver;
2020

2121
class CheckboxType extends AbstractType
2222
{
2323
/**
2424
* {@inheritdoc}
2525
*/
26-
public function buildForm(FormBuilder $builder, array $options)
26+
public function buildForm(FormBuilderInterface $builder, array $options)
2727
{
2828
$builder
2929
->appendClientTransformer(new BooleanToStringTransformer($options['value']))
@@ -33,7 +33,7 @@ public function buildForm(FormBuilder $builder, array $options)
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
public function buildView(FormView $view, FormInterface $form, array $options)
36+
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
3737
{
3838
$view
3939
->set('value', $options['value'])
@@ -60,7 +60,7 @@ public function setDefaultOptions(OptionsResolver $resolver)
6060
/**
6161
* {@inheritdoc}
6262
*/
63-
public function getParent(array $options)
63+
public function getParent()
6464
{
6565
return 'field';
6666
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15-
use Symfony\Component\Form\FormBuilder;
15+
use Symfony\Component\Form\FormBuilderInterface;
1616
use Symfony\Component\Form\FormInterface;
17-
use Symfony\Component\Form\FormView;
17+
use Symfony\Component\Form\FormViewInterface;
1818
use Symfony\Component\Form\Exception\FormException;
1919
use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceList;
2020
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
@@ -34,7 +34,7 @@ class ChoiceType extends AbstractType
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function buildForm(FormBuilder $builder, array $options)
37+
public function buildForm(FormBuilderInterface $builder, array $options)
3838
{
3939
if (!$options['choice_list'] && !is_array($options['choices']) && !$options['choices'] instanceof \Traversable) {
4040
throw new FormException('Either the option "choices" or "choice_list" must be set.');
@@ -73,7 +73,7 @@ public function buildForm(FormBuilder $builder, array $options)
7373
/**
7474
* {@inheritdoc}
7575
*/
76-
public function buildView(FormView $view, FormInterface $form, array $options)
76+
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
7777
{
7878
$view
7979
->set('multiple', $options['multiple'])
@@ -95,7 +95,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
9595
/**
9696
* {@inheritdoc}
9797
*/
98-
public function buildViewBottomUp(FormView $view, FormInterface $form, array $options)
98+
public function finishView(FormViewInterface $view, FormInterface $form, array $options)
9999
{
100100
if ($options['expanded']) {
101101
// Radio buttons should have the same name as the parent
@@ -178,7 +178,7 @@ public function setDefaultOptions(OptionsResolver $resolver)
178178
/**
179179
* {@inheritdoc}
180180
*/
181-
public function getParent(array $options)
181+
public function getParent()
182182
{
183183
return 'field';
184184
}
@@ -194,11 +194,11 @@ public function getName()
194194
/**
195195
* Adds the sub fields for an expanded choice field.
196196
*
197-
* @param FormBuilder $builder The form builder.
198-
* @param array $choiceViews The choice view objects.
199-
* @param array $options The build options.
197+
* @param FormBuilderInterface $builder The form builder.
198+
* @param array $choiceViews The choice view objects.
199+
* @param array $options The build options.
200200
*/
201-
private function addSubForms(FormBuilder $builder, array $choiceViews, array $options)
201+
private function addSubForms(FormBuilderInterface $builder, array $choiceViews, array $options)
202202
{
203203
foreach ($choiceViews as $i => $choiceView) {
204204
if (is_array($choiceView)) {

0 commit comments

Comments
 (0)
0