Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 61d3be0

Browse files
committed
merged branch bschussek/rename-bind (PR #7736)
This PR was merged into the master branch. Discussion ---------- [Form] Deprecated bind() and isBound() in favor of submit() and isSubmitted() | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | yes (*) | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #5493 | License | MIT | Doc PR | TODO This change was discussed for a while in #5493. **(*)** It breaks BC *only for people who implemented* `FormInterface` *manually* (not a lot, so I hope). These can fix the problem by simply renaming `bind()` and `isBound()` in their implementation to `submit()` and `isSubmitted()`. The main rationale is that with the request handlers introduced in #6522, people won't be confronted with the term "binding" anymore. As such, `isBound()` will be a very strange name to new users that have never used `bind()` manually. See this code sample as example: ```php $form = $this->createForm(...); $form->handleRequest($request); // Imagine you have never heard about bind() or binding. What does this mean? if ($form->isBound()) { // ... } ``` In reality, `bind()` submits a form. Where-ever I renamed "bind" to "submit" in the comments, "submit" made actually much more sense. So it does in the code sample above: ```php $form = $this->createForm(...); $form->handleRequest($request); // Aha! if ($form->isSubmitted()) { // ... } ``` Also when using `submit()` directly, the code makes much more sense now: ```php $text = $this->createForm('text'); $text->submit('New Value'); ``` For current users, the current naming will be supported until 3.0. Commits ------- 41b0127 [Form] Deprecated bind() and isBound() in favor of submit() and isSubmitted()
2 parents 81f37ba + 41b0127 commit 61d3be0

File tree

61 files changed

+719
-520
lines changed

Some content is hidden

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

61 files changed

+719
-520
lines changed

UPGRADE-3.0.md

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,25 @@ UPGRADE FROM 2.x to 3.0
2020

2121
### Form
2222

23-
* Passing a `Symfony\Component\HttpFoundation\Request` instance to
24-
`FormInterface::bind()` was disabled. You should use
25-
`FormInterface::handleRequest()` instead.
23+
* The methods `Form::bind()` and `Form::isBound()` were removed. You should
24+
use `Form::submit()` and `Form::isSubmitted()` instead.
25+
26+
Before:
27+
28+
```
29+
$form->bind(array(...));
30+
```
31+
32+
After:
33+
34+
```
35+
$form->submit(array(...));
36+
```
37+
38+
* Passing a `Symfony\Component\HttpFoundation\Request` instance, as was
39+
supported by `FormInterface::bind()`, is not possible with
40+
`FormInterface::submit()` anymore. You should use `FormInterface::handleRequest()`
41+
instead.
2642

2743
Before:
2844

@@ -39,20 +55,20 @@ UPGRADE FROM 2.x to 3.0
3955
After:
4056

4157
```
42-
$form->handleRequest();
58+
$form->handleRequest($request);
4359
4460
if ($form->isValid()) {
4561
// ...
4662
}
4763
```
4864

4965
If you want to test whether the form was submitted separately, you can use
50-
the method `isBound()`:
66+
the method `isSubmitted()`:
5167

5268
```
53-
$form->handleRequest();
69+
$form->handleRequest($request);
5470
55-
if ($form->isBound()) {
71+
if ($form->isSubmitted()) {
5672
// ...
5773
5874
if ($form->isValid()) {
@@ -61,6 +77,25 @@ UPGRADE FROM 2.x to 3.0
6177
}
6278
```
6379

80+
* The events PRE_BIND, BIND and POST_BIND were renamed to PRE_SUBMIT, SUBMIT
81+
and POST_SUBMIT.
82+
83+
Before:
84+
85+
```
86+
$builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) {
87+
// ...
88+
});
89+
```
90+
91+
After:
92+
93+
```
94+
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
95+
// ...
96+
});
97+
```
98+
6499
* The option "virtual" was renamed to "inherit_data".
65100

66101
Before:

src/Symfony/Bridge/Doctrine/Form/EventListener/MergeDoctrineCollectionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function getSubscribedEvents()
3030
{
3131
// Higher priority than core MergeCollectionListener so that this one
3232
// is called before
33-
return array(FormEvents::BIND => array('onBind', 10));
33+
return array(FormEvents::SUBMIT => array('onBind', 10));
3434
}
3535

3636
public function onBind(FormEvent $event)

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
196196
},
197197
));
198198

199-
$field->bind('2');
199+
$field->submit('2');
200200
}
201201

202202
public function testSetDataSingleNull()
@@ -248,7 +248,7 @@ public function testSubmitSingleExpandedNull()
248248
'em' => 'default',
249249
'class' => self::SINGLE_IDENT_CLASS,
250250
));
251-
$field->bind(null);
251+
$field->submit(null);
252252

253253
$this->assertNull($field->getData());
254254
$this->assertSame(array(), $field->getViewData());
@@ -262,7 +262,7 @@ public function testSubmitSingleNonExpandedNull()
262262
'em' => 'default',
263263
'class' => self::SINGLE_IDENT_CLASS,
264264
));
265-
$field->bind(null);
265+
$field->submit(null);
266266

267267
$this->assertNull($field->getData());
268268
$this->assertSame('', $field->getViewData());
@@ -275,7 +275,7 @@ public function testSubmitMultipleNull()
275275
'em' => 'default',
276276
'class' => self::SINGLE_IDENT_CLASS,
277277
));
278-
$field->bind(null);
278+
$field->submit(null);
279279

280280
$this->assertEquals(new ArrayCollection(), $field->getData());
281281
$this->assertSame(array(), $field->getViewData());
@@ -296,7 +296,7 @@ public function testSubmitSingleNonExpandedSingleIdentifier()
296296
'property' => 'name',
297297
));
298298

299-
$field->bind('2');
299+
$field->submit('2');
300300

301301
$this->assertTrue($field->isSynchronized());
302302
$this->assertSame($entity2, $field->getData());
@@ -319,7 +319,7 @@ public function testSubmitSingleNonExpandedCompositeIdentifier()
319319
));
320320

321321
// the collection key is used here
322-
$field->bind('1');
322+
$field->submit('1');
323323

324324
$this->assertTrue($field->isSynchronized());
325325
$this->assertSame($entity2, $field->getData());
@@ -342,7 +342,7 @@ public function testSubmitMultipleNonExpandedSingleIdentifier()
342342
'property' => 'name',
343343
));
344344

345-
$field->bind(array('1', '3'));
345+
$field->submit(array('1', '3'));
346346

347347
$expected = new ArrayCollection(array($entity1, $entity3));
348348

@@ -370,7 +370,7 @@ public function testSubmitMultipleNonExpandedSingleIdentifierForExistingData()
370370
$existing = new ArrayCollection(array(0 => $entity2));
371371

372372
$field->setData($existing);
373-
$field->bind(array('1', '3'));
373+
$field->submit(array('1', '3'));
374374

375375
// entry with index 0 ($entity2) was replaced
376376
$expected = new ArrayCollection(array(0 => $entity1, 1 => $entity3));
@@ -399,7 +399,7 @@ public function testSubmitMultipleNonExpandedCompositeIdentifier()
399399
));
400400

401401
// because of the composite key collection keys are used
402-
$field->bind(array('0', '2'));
402+
$field->submit(array('0', '2'));
403403

404404
$expected = new ArrayCollection(array($entity1, $entity3));
405405

@@ -427,7 +427,7 @@ public function testSubmitMultipleNonExpandedCompositeIdentifierExistingData()
427427
$existing = new ArrayCollection(array(0 => $entity2));
428428

429429
$field->setData($existing);
430-
$field->bind(array('0', '2'));
430+
$field->submit(array('0', '2'));
431431

432432
// entry with index 0 ($entity2) was replaced
433433
$expected = new ArrayCollection(array(0 => $entity1, 1 => $entity3));
@@ -454,7 +454,7 @@ public function testSubmitSingleExpanded()
454454
'property' => 'name',
455455
));
456456

457-
$field->bind('2');
457+
$field->submit('2');
458458

459459
$this->assertTrue($field->isSynchronized());
460460
$this->assertSame($entity2, $field->getData());
@@ -480,7 +480,7 @@ public function testSubmitMultipleExpanded()
480480
'property' => 'name',
481481
));
482482

483-
$field->bind(array('1', '3'));
483+
$field->submit(array('1', '3'));
484484

485485
$expected = new ArrayCollection(array($entity1, $entity3));
486486

@@ -510,7 +510,7 @@ public function testOverrideChoices()
510510
'property' => 'name',
511511
));
512512

513-
$field->bind('2');
513+
$field->submit('2');
514514

515515
$this->assertEquals(array(1 => new ChoiceView($entity1, '1', 'Foo'), 2 => new ChoiceView($entity2, '2', 'Bar')), $field->createView()->vars['choices']);
516516
$this->assertTrue($field->isSynchronized());
@@ -535,7 +535,7 @@ public function testGroupByChoices()
535535
'group_by' => 'groupName',
536536
));
537537

538-
$field->bind('2');
538+
$field->submit('2');
539539

540540
$this->assertSame('2', $field->getViewData());
541541
$this->assertEquals(array(
@@ -599,7 +599,7 @@ public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier()
599599
'property' => 'name',
600600
));
601601

602-
$field->bind('3');
602+
$field->submit('3');
603603

604604
$this->assertFalse($field->isSynchronized());
605605
$this->assertNull($field->getData());
@@ -620,7 +620,7 @@ public function testDisallowChoicesThatAreNotIncludedChoicesCompositeIdentifier(
620620
'property' => 'name',
621621
));
622622

623-
$field->bind('2');
623+
$field->submit('2');
624624

625625
$this->assertFalse($field->isSynchronized());
626626
$this->assertNull($field->getData());
@@ -644,7 +644,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifie
644644
'property' => 'name',
645645
));
646646

647-
$field->bind('3');
647+
$field->submit('3');
648648

649649
$this->assertFalse($field->isSynchronized());
650650
$this->assertNull($field->getData());
@@ -668,7 +668,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingle
668668
'property' => 'name',
669669
));
670670

671-
$field->bind('3');
671+
$field->submit('3');
672672

673673
$this->assertFalse($field->isSynchronized());
674674
$this->assertNull($field->getData());
@@ -692,7 +692,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompos
692692
'property' => 'name',
693693
));
694694

695-
$field->bind('2');
695+
$field->submit('2');
696696

697697
$this->assertFalse($field->isSynchronized());
698698
$this->assertNull($field->getData());
@@ -712,7 +712,7 @@ public function testSubmitSingleStringIdentifier()
712712
'property' => 'name',
713713
));
714714

715-
$field->bind('foo');
715+
$field->submit('foo');
716716

717717
$this->assertTrue($field->isSynchronized());
718718
$this->assertSame($entity1, $field->getData());
@@ -734,7 +734,7 @@ public function testSubmitCompositeStringIdentifier()
734734
));
735735

736736
// the collection key is used here
737-
$field->bind('0');
737+
$field->submit('0');
738738

739739
$this->assertTrue($field->isSynchronized());
740740
$this->assertSame($entity1, $field->getData());

src/Symfony/Component/Form/Button.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Form;
1313

14-
use Symfony\Component\Form\Exception\AlreadyBoundException;
14+
use Symfony\Component\Form\Exception\AlreadySubmittedException;
1515
use Symfony\Component\Form\Exception\BadMethodCallException;
1616

1717
/**
@@ -34,7 +34,7 @@ class Button implements \IteratorAggregate, FormInterface
3434
/**
3535
* @var Boolean
3636
*/
37-
private $bound = false;
37+
private $submitted = false;
3838

3939
/**
4040
* Creates a new button from a form configuration.
@@ -258,9 +258,9 @@ public function getConfig()
258258
*
259259
* @return Boolean true if the button was submitted.
260260
*/
261-
public function isBound()
261+
public function isSubmitted()
262262
{
263-
return $this->bound;
263+
return $this->submitted;
264264
}
265265

266266
/**
@@ -356,21 +356,21 @@ public function handleRequest($request = null)
356356
}
357357

358358
/**
359-
* Binds data to the button.
359+
* Submits data to the button.
360360
*
361361
* @param null|string $submittedData The data
362362
*
363363
* @return Button The button instance
364364
*
365-
* @throws Exception\AlreadyBoundException If the form has already been bound.
365+
* @throws Exception\AlreadySubmittedException If the button has already been submitted.
366366
*/
367-
public function bind($submittedData)
367+
public function submit($submittedData)
368368
{
369-
if ($this->bound) {
370-
throw new AlreadyBoundException('A form can only be bound once');
369+
if ($this->submitted) {
370+
throw new AlreadySubmittedException('A form can only be submitted once');
371371
}
372372

373-
$this->bound = true;
373+
$this->submitted = true;
374374

375375
return $this;
376376
}

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ CHANGELOG
1818
* added component-level exceptions for various SPL exceptions
1919
changed all uses of the deprecated Exception class to use more specialized exceptions instead
2020
removed NotInitializedException, NotValidException, TypeDefinitionException, TypeLoaderException, CreationException
21+
* added events PRE_SUBMIT, SUBMIT and POST_SUBMIT
22+
* deprecated events PRE_BIND, BIND and POST_BIND
23+
* [BC BREAK] renamed bind() and isBound() in FormInterface to submit() and isSubmitted()
24+
* added methods submit() and isSubmitted() to Form
25+
* deprecated bind() and isBound() in Form
26+
* deprecated AlreadyBoundException in favor of AlreadySubmittedException
2127

2228
2.2.0
2329
-----

src/Symfony/Component/Form/DataTransformerInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ interface DataTransformerInterface
2424
* This method is called on two occasions inside a form field:
2525
*
2626
* 1. When the form field is initialized with the data attached from the datasource (object or array).
27-
* 2. When data from a request is bound using {@link Form::bind()} to transform the new input data
28-
* back into the renderable format. For example if you have a date field and bind '2009-10-10' onto
29-
* it you might accept this value because its easily parsed, but the transformer still writes back
27+
* 2. When data from a request is submitted using {@link Form::submit()} to transform the new input data
28+
* back into the renderable format. For example if you have a date field and submit '2009-10-10'
29+
* you might accept this value because its easily parsed, but the transformer still writes back
3030
* "2009/10/10" onto the form field (for further displaying or other purposes).
3131
*
3232
* This method must be able to deal with empty values. Usually this will
@@ -52,7 +52,7 @@ public function transform($value);
5252
* Transforms a value from the transformed representation to its original
5353
* representation.
5454
*
55-
* This method is called when {@link Form::bind()} is called to transform the requests tainted data
55+
* This method is called when {@link Form::submit()} is called to transform the requests tainted data
5656
* into an acceptable format for your data processing/model layer.
5757
*
5858
* This method must be able to deal with empty values. Usually this will

src/Symfony/Component/Form/Exception/AlreadyBoundException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
namespace Symfony\Component\Form\Exception;
1313

14+
/**
15+
* Alias of {@link AlreadySubmittedException}.
16+
*
17+
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
18+
* {@link AlreadySubmittedException} instead.
19+
*/
1420
class AlreadyBoundException extends LogicException
1521
{
1622
}

0 commit comments

Comments
 (0)