8000 [Form] Enhanced the form error message · symfony/symfony@b5cf337 · GitHub
[go: up one dir, main page]

Skip to content

Commit b5cf337

Browse files
author
Bart van den Burg
committed
[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.
1 parent 66ff060 commit b5cf337

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,25 +348,27 @@ public function setData($modelData)
348348
if (!empty($viewData)) {
349349
$dataClass = $this->config->getDataClass();
350350

351+
$actualType = is_object($viewData) ? 'an instance of class ' . get_class($viewData) : ' a(n) ' . gettype($viewData);
352+
351353
if (null === $dataClass && is_object($viewData) && !$viewData instanceof \ArrayAccess) {
352354
$expectedType = 'scalar, array or an instance of \ArrayAccess';
353355

354356
throw new FormException(
355357
'The form\'s view data is expected to be of type ' . $expectedType . ', ' .
356-
'but is an instance of class ' . get_class($viewData) . '. You ' .
358+
'but is ' . $actualType . '. You ' .
357359
'can avoid this error by setting the "data_class" option to ' .
358360
'"' . get_class($viewData) . '" or by adding a view transformer ' .
359-
'that transforms ' . get_class($viewData) . ' to ' . $expectedType . '.'
361+
'that transforms ' . $actualType . ' to ' . $expectedType . '.'
360362
);
361363
}
362364

363365
if (null !== $dataClass && !$viewData instanceof $dataClass) {
364366
throw new FormException(
365367
'The form\'s view data is expected to be an instance of class ' .
366-
$dataClass . ', but has the type ' . gettype($viewData) . '. You ' .
367-
'can avoid this error by setting the "data_class" option to ' .
368-
'null or by adding a view transformer that transforms ' .
369-
gettype($viewData) . ' to ' . $dataClass . '.'
368+
$dataClass . ', but is '. $actualType . '. You can avoid this error ' .
369 5CC2 +
'by setting the "data_class" option to null or by adding a view ' .
370+
'transformer that transforms ' . $actualType . ' to an instance of ' .
371+
$dataClass . '.'
370372
);
371373
}
372374
}

0 commit comments

Comments
 (0)
0