From 7bc7a8a6ec83cd9f1b7efd5a1889a28763063eb6 Mon Sep 17 00:00:00 2001 From: Matt Janssen Date: Mon, 30 Sep 2013 22:14:37 -0500 Subject: [PATCH 1/2] [Form] Accept interfaces to be passed to "data_class" --- src/Symfony/Component/Form/FormConfigBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/FormConfigBuilder.php b/src/Symfony/Component/Form/FormConfigBuilder.php index 1015da4f51..391c5cd5e2 100644 --- a/src/Symfony/Component/Form/FormConfigBuilder.php +++ b/src/Symfony/Component/Form/FormConfigBuilder.php @@ -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)); } From b44e07b7f6badf646a3a6758b5817eea8234d915 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 28 Mar 2014 12:44:24 +0100 Subject: [PATCH 2/2] [Form] Added test case for 4759e062ed004749dbdc2ba31aef0f8ac2601895 --- src/Symfony/Component/Form/CHANGELOG.md | 1 + .../Extension/Core/Type/FormTypeTest.php | 31 +++++++++++++++++++ .../Form/Tests/Fixtures/AbstractAuthor.php | 16 ++++++++++ .../Form/Tests/Fixtures/AuthorInterface.php | 16 ++++++++++ 4 files changed, 64 insertions(+) create mode 100644 src/Symfony/Component/Form/Tests/Fixtures/AbstractAuthor.php create mode 100644 src/Symfony/Component/Form/Tests/Fixtures/AuthorInterface.php diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index c210f9e792..dc7a59bbde 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -11,6 +11,7 @@ CHANGELOG * [BC BREAK] added two optional parameters to FormInterface::getErrors() and changed the method to return a Symfony\Component\Form\FormErrorIterator instance instead of an array + * you can now pass interface names in the "data_class" option 2.4.0 ----- diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php index 6a6a17d84a..a8465dc990 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php @@ -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( diff --git a/src/Symfony/Component/Form/Tests/Fixtures/AbstractAuthor.php b/src/Symfony/Component/Form/Tests/Fixtures/AbstractAuthor.php new file mode 100644 index 0000000000..03a6b724f3 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/AbstractAuthor.php @@ -0,0 +1,16 @@ + + * + * 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 +{ +} diff --git a/src/Symfony/Component/Form/Tests/Fixtures/AuthorInterface.php b/src/Symfony/Component/Form/Tests/Fixtures/AuthorInterface.php new file mode 100644 index 0000000000..984cb541ec --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/AuthorInterface.php @@ -0,0 +1,16 @@ + + * + * 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 +{ +}