[Form] Enhanced the form error message

The error message on type mismatch is a bit obscure:

The form's view data is expected to be an instance of class Samson\Bundle\TRSBundle\Entity\Labour, but has the type object. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms object to Samson\Bundle\TRSBundle\Entity\Labour.

This commit changes it to:

The form's view data is expected to be an instance of class Samson\Bundle\TRSBundle\Entity\Labour, but is an instance of Closure. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of Closure to an instance of Samson\Bundle\TRSBundle\Entity\Labour.
This commit is contained in:
Bart van den Burg 2012-06-12 15:56:59 +02:00
parent 66ff06096c
commit b5cf337c9c

View File

@ -348,25 +348,27 @@ class Form implements \IteratorAggregate, FormInterface
if (!empty($viewData)) { if (!empty($viewData)) {
$dataClass = $this->config->getDataClass(); $dataClass = $this->config->getDataClass();
$actualType = is_object($viewData) ? 'an instance of class ' . get_class($viewData) : ' a(n) ' . gettype($viewData);
if (null === $dataClass && is_object($viewData) && !$viewData instanceof \ArrayAccess) { if (null === $dataClass && is_object($viewData) && !$viewData instanceof \ArrayAccess) {
$expectedType = 'scalar, array or an instance of \ArrayAccess'; $expectedType = 'scalar, array or an instance of \ArrayAccess';
throw new FormException( throw new FormException(
'The form\'s view data is expected to be of type ' . $expectedType . ', ' . 'The form\'s view data is expected to be of type ' . $expectedType . ', ' .
'but is an instance of class ' . get_class($viewData) . '. You ' . 'but is ' . $actualType . '. You ' .
'can avoid this error by setting the "data_class" option to ' . 'can avoid this error by setting the "data_class" option to ' .
'"' . get_class($viewData) . '" or by adding a view transformer ' . '"' . get_class($viewData) . '" or by adding a view transformer ' .
'that transforms ' . get_class($viewData) . ' to ' . $expectedType . '.' 'that transforms ' . $actualType . ' to ' . $expectedType . '.'
); );
} }
if (null !== $dataClass && !$viewData instanceof $dataClass) { if (null !== $dataClass && !$viewData instanceof $dataClass) {
throw new FormException( throw new FormException(
'The form\'s view data is expected to be an instance of class ' . 'The form\'s view data is expected to be an instance of class ' .
$dataClass . ', but has the type ' . gettype($viewData) . '. You ' . $dataClass . ', but is '. $actualType . '. You can avoid this error ' .
'can avoid this error by setting the "data_class" option to ' . 'by setting the "data_class" option to null or by adding a view ' .
'null or by adding a view transformer that transforms ' . 'transformer that transforms ' . $actualType . ' to an instance of ' .
gettype($viewData) . ' to ' . $dataClass . '.' $dataClass . '.'
); );
} }
} }