Skip to content

Commit

Permalink
feature #10569 [Form] Accept interfaces to be passed to "data_class" …
Browse files Browse the repository at this point in the history
…(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"
  • Loading branch information
fabpot committed Mar 31, 2014
2 parents c5a3008 + b44e07b commit 880880b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -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
-----
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormConfigBuilder.php
Expand Up @@ -192,7 +192,7 @@ public function __construct($name, $dataClass, EventDispatcherInterface $dispatc
{
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));
}

Expand Down
Expand Up @@ -146,6 +146,37 @@ public function testPassMaxLengthBCToView()
$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(
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Form/Tests/Fixtures/AbstractAuthor.php
@@ -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
{
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/Form/Tests/Fixtures/AuthorInterface.php
@@ -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
{
}

0 comments on commit 880880b

Please sign in to comment.