diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 63ab323523..2999e1ffcf 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -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 ----- 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)); } 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 +{ +}