Skip to content

[Form] Deprecated bind() and isBound() in favor of submit() and isSubmitted() #7736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@ UPGRADE FROM 2.x to 3.0

### Form

* Passing a `Symfony\Component\HttpFoundation\Request` instance to
`FormInterface::bind()` was disabled. You should use
`FormInterface::handleRequest()` instead.
* The methods `Form::bind()` and `Form::isBound()` were removed. You should
use `Form::submit()` and `Form::isSubmitted()` instead.

Before:

```
$form->bind(array(...));
```

After:

```
$form->submit(array(...));
```

* Passing a `Symfony\Component\HttpFoundation\Request` instance, as was
supported by `FormInterface::bind()`, is not possible with
`FormInterface::submit()` anymore. You should use `FormInterface::handleRequest()`
instead.

Before:

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

```
$form->handleRequest();
$form->handleRequest($request);

if ($form->isValid()) {
// ...
}
```

If you want to test whether the form was submitted separately, you can use
the method `isBound()`:
the method `isSubmitted()`:

```
$form->handleRequest();
$form->handleRequest($request);

if ($form->isBound()) {
if ($form->isSubmitted()) {
// ...

if ($form->isValid()) {
Expand All @@ -61,6 +77,25 @@ UPGRADE FROM 2.x to 3.0
}
```

* The events PRE_BIND, BIND and POST_BIND were renamed to PRE_SUBMIT, SUBMIT
and POST_SUBMIT.

Before:

```
$builder->addEventListener(FormEvents::PRE_BIND, function (FormEvent $event) {
// ...
});
```

After:

```
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
// ...
});
```

* The option "virtual" was renamed to "inherit_data".

Before:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function getSubscribedEvents()
{
// Higher priority than core MergeCollectionListener so that this one
// is called before
return array(FormEvents::BIND => array('onBind', 10));
return array(FormEvents::SUBMIT => array('onBind', 10));
}

public function onBind(FormEvent $event)
Expand Down
42 changes: 21 additions & 21 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
},
));

$field->bind('2');
$field->submit('2');
}

public function testSetDataSingleNull()
Expand Down Expand Up @@ -248,7 +248,7 @@ public function testSubmitSingleExpandedNull()
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
));
$field->bind(null);
$field->submit(null);

$this->assertNull($field->getData());
$this->assertSame(array(), $field->getViewData());
Expand All @@ -262,7 +262,7 @@ public function testSubmitSingleNonExpandedNull()
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
));
$field->bind(null);
$field->submit(null);

$this->assertNull($field->getData());
$this->assertSame('', $field->getViewData());
Expand All @@ -275,7 +275,7 @@ public function testSubmitMultipleNull()
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
));
$field->bind(null);
$field->submit(null);

$this->assertEquals(new ArrayCollection(), $field->getData());
$this->assertSame(array(), $field->getViewData());
Expand All @@ -296,7 +296,7 @@ public function testSubmitSingleNonExpandedSingleIdentifier()
'property' => 'name',
));

$field->bind('2');
$field->submit('2');

$this->assertTrue($field->isSynchronized());
$this->assertSame($entity2, $field->getData());
Expand All @@ -319,7 +319,7 @@ public function testSubmitSingleNonExpandedCompositeIdentifier()
));

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

$this->assertTrue($field->isSynchronized());
$this->assertSame($entity2, $field->getData());
Expand All @@ -342,7 +342,7 @@ public function testSubmitMultipleNonExpandedSingleIdentifier()
'property' => 'name',
));

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

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

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

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

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

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

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

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

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

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

$field->bind('2');
$field->submit('2');

$this->assertTrue($field->isSynchronized());
$this->assertSame($entity2, $field->getData());
Expand All @@ -480,7 +480,7 @@ public function testSubmitMultipleExpanded()
'property' => 'name',
));

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

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

Expand Down Expand Up @@ -510,7 +510,7 @@ public function testOverrideChoices()
'property' => 'name',
));

$field->bind('2');
$field->submit('2');

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

$field->bind('2');
$field->submit('2');

$this->assertSame('2', $field->getViewData());
$this->assertEquals(array(
Expand Down Expand Up @@ -599,7 +599,7 @@ public function testDisallowChoicesThatAreNotIncludedChoicesSingleIdentifier()
'property' => 'name',
));

$field->bind('3');
$field->submit('3');

$this->assertFalse($field->isSynchronized());
$this->assertNull($field->getData());
Expand All @@ -620,7 +620,7 @@ public function testDisallowChoicesThatAreNotIncludedChoicesCompositeIdentifier(
'property' => 'name',
));

$field->bind('2');
$field->submit('2');

$this->assertFalse($field->isSynchronized());
$this->assertNull($field->getData());
Expand All @@ -644,7 +644,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifie
'property' => 'name',
));

$field->bind('3');
$field->submit('3');

$this->assertFalse($field->isSynchronized());
$this->assertNull($field->getData());
Expand All @@ -668,7 +668,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingle
'property' => 'name',
));

$field->bind('3');
$field->submit('3');

$this->assertFalse($field->isSynchronized());
$this->assertNull($field->getData());
Expand All @@ -692,7 +692,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompos
'property' => 'name',
));

$field->bind('2');
$field->submit('2');

$this->assertFalse($field->isSynchronized());
$this->assertNull($field->getData());
Expand All @@ -712,7 +712,7 @@ public function testSubmitSingleStringIdentifier()
'property' => 'name',
));

$field->bind('foo');
$field->submit('foo');

$this->assertTrue($field->isSynchronized());
$this->assertSame($entity1, $field->getData());
Expand All @@ -734,7 +734,7 @@ public function testSubmitCompositeStringIdentifier()
));

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

$this->assertTrue($field->isSynchronized());
$this->assertSame($entity1, $field->getData());
Expand Down
20 changes: 10 additions & 10 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\AlreadyBoundException;
use Symfony\Component\Form\Exception\AlreadySubmittedException;
use Symfony\Component\Form\Exception\BadMethodCallException;

/**
Expand All @@ -34,7 +34,7 @@ class Button implements \IteratorAggregate, FormInterface
/**
* @var Boolean
*/
private $bound = false;
private $submitted = false;

/**
* Creates a new button from a form configuration.
Expand Down Expand Up @@ -258,9 +258,9 @@ public function getConfig()
*
* @return Boolean true if the button was submitted.
*/
public function isBound()
public function isSubmitted()
{
return $this->bound;
return $this->submitted;
}

/**
Expand Down Expand Up @@ -356,21 +356,21 @@ public function handleRequest($request = null)
}

/**
* Binds data to the button.
* Submits data to the button.
*
* @param null|string $submittedData The data
*
* @return Button The button instance
*
* @throws Exception\AlreadyBoundException If the form has already been bound.
* @throws Exception\AlreadySubmittedException If the button has already been submitted.
*/
public function bind($submittedData)
public function submit($submittedData)
{
if ($this->bound) {
throw new AlreadyBoundException('A form can only be bound once');
if ($this->submitted) {
throw new AlreadySubmittedException('A form can only be submitted once');
}

$this->bound = true;
$this->submitted = true;

return $this;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ CHANGELOG
* added component-level exceptions for various SPL exceptions
changed all uses of the deprecated Exception class to use more specialized exceptions instead
removed NotInitializedException, NotValidException, TypeDefinitionException, TypeLoaderException, CreationException
* added events PRE_SUBMIT, SUBMIT and POST_SUBMIT
* deprecated events PRE_BIND, BIND and POST_BIND
* [BC BREAK] renamed bind() and isBound() in FormInterface to submit() and isSubmitted()
* added methods submit() and isSubmitted() to Form
* deprecated bind() and isBound() in Form
* deprecated AlreadyBoundException in favor of AlreadySubmittedException

2.2.0
-----
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Form/DataTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ interface DataTransformerInterface
* This method is called on two occasions inside a form field:
*
* 1. When the form field is initialized with the data attached from the datasource (object or array).
* 2. When data from a request is bound using {@link Form::bind()} to transform the new input data
* back into the renderable format. For example if you have a date field and bind '2009-10-10' onto
* it you might accept this value because its easily parsed, but the transformer still writes back
* 2. When data from a request is submitted using {@link Form::submit()} to transform the new input data
* back into the renderable format. For example if you have a date field and submit '2009-10-10'
* you might accept this value because its easily parsed, but the transformer still writes back
* "2009/10/10" onto the form field (for further displaying or other purposes).
*
* This method must be able to deal with empty values. Usually this will
Expand All @@ -52,7 +52,7 @@ public function transform($value);
* Transforms a value from the transformed representation to its original
* representation.
*
* This method is called when {@link Form::bind()} is called to transform the requests tainted data
* This method is called when {@link Form::submit()} is called to transform the requests tainted data
* into an acceptable format for your data processing/model layer.
*
* This method must be able to deal with empty values. Usually this will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

namespace Symfony\Component\Form\Exception;

/**
* Alias of {@link AlreadySubmittedException}.
*
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
* {@link AlreadySubmittedException} instead.
*/
class AlreadyBoundException extends LogicException
{
}
Loading