merged branch Burgov/add_error_on_wrong_type (PR #2859)

Commits
-------

d97d7e9 Added a check to see if the type is a string if it's not a FormTypeInterface

Discussion
----------

Add exception on wrong type

When you forget to extend AbstractType in your form type, and then try to create a named builder from it, the error message is quite confusing:

Expected argument of type "string", "Samson\InvoiceBundle\Form\Type\PaymentTermsType" given (from the getType() method)

This PR checks for the right type at the relevant place

---------------------------------------------------------------------------

by stloyd at 2011/12/13 03:00:29 -0800

IMO you should add an test for this.

---------------------------------------------------------------------------

by Burgov at 2011/12/13 03:11:50 -0800

@stloyd done

---------------------------------------------------------------------------

by fabpot at 2011/12/13 03:27:08 -0800

@Burgov: Looks good to me. Can you squash your commits before I merge this PR? Thanks.

---------------------------------------------------------------------------

by Burgov at 2011/12/13 03:29:00 -0800

@fabpot done!
This commit is contained in:
Fabien Potencier 2011-12-13 12:29:53 +01:00
commit 9e97e687b7
2 changed files with 12 additions and 1 deletions

View File

@ -225,8 +225,10 @@ class FormFactory implements FormFactoryInterface
}
$this->addType($type);
} else {
} elseif (is_string($type)) {
$type = $this->getType($type);
} else {
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
}
$defaultOptions = $type->getDefaultOptions($options);

View File

@ -320,6 +320,15 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($this->factory->hasType('foo'));
}
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedExceptionMessage Expected argument of type "string or Symfony\Component\Form\FormTypeInterface", "stdClass" given
*/
public function testCreateNamedBuilderThrowsUnderstandableException()
{
$this->factory->createNamedBuilder(new \StdClass, 'name');
}
public function testCreateUsesTypeNameAsName()
{
$type = new FooType();