feature #10569 [Form] Accept interfaces to be passed to "data_class" (webmozart)

This PR was merged into the 2.5-dev branch.

Discussion
----------

[Form] Accept interfaces to be passed to "data_class"

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8669
| License       | MIT
| Doc PR        | -

Commits
-------

b44e07b [Form] Added test case for 4759e062ed004749dbdc2ba31aef0f8ac2601895
7bc7a8a [Form] Accept interfaces to be passed to "data_class"
This commit is contained in:
Fabien Potencier 2014-03-31 13:06:03 +02:00
commit 880880b838
5 changed files with 65 additions and 1 deletions

View File

@ -14,6 +14,7 @@ CHANGELOG
* errors mapped to unsubmitted forms are discarded now
* ObjectChoiceList now compares choices by their value, if a value path is
given
* you can now pass interface names in the "data_class" option
2.4.0
-----

View File

@ -192,7 +192,7 @@ class FormConfigBuilder implements FormConfigBuilderInterface
{
self::validateName($name);
if (null !== $dataClass && !class_exists($dataClass)) {
if (null !== $dataClass && !class_exists($dataClass) && !interface_exists($dataClass)) {
throw new InvalidArgumentException(sprintf('The data class "%s" is not a valid class.', $dataClass));
}

View File

@ -146,6 +146,37 @@ class FormTypeTest extends BaseTypeTest
$this->assertSame(10, $view->vars['attr']['maxlength']);
}
public function testDataClassMayBeNull()
{
$this->factory->createBuilder('form', null, array(
'data_class' => null,
));
}
public function testDataClassMayBeAbstractClass()
{
$this->factory->createBuilder('form', null, array(
'data_class' => 'Symfony\Component\Form\Tests\Fixtures\AbstractAuthor',
));
}
public function testDataClassMayBeInterface()
{
$this->factory->createBuilder('form', null, array(
'data_class' => 'Symfony\Component\Form\Tests\Fixtures\AuthorInterface',
));
}
/**
* @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
*/
public function testDataClassMustBeValidClassOrInterface()
{
$this->factory->createBuilder('form', null, array(
'data_class' => 'foobar',
));
}
public function testSubmitWithEmptyDataCreatesObjectIfClassAvailable()
{
$builder = $this->factory->createBuilder('form', null, array(

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\Fixtures;
abstract class AbstractAuthor
{
}

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\Fixtures;
interface AuthorInterface
{
}