8000 minor #25492 improve FormType::getType exception message details (Nic… · symfony/symfony@3d9676c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3d9676c

Browse files
minor #25492 improve FormType::getType exception message details (Nicals)
This PR was submitted for the master branch but it was squashed and merged into the 2.8 branch instead (closes #25492). Discussion ---------- improve FormType::getType exception message details | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | This pull request improves the exception thrown when a Type is requested from Symfony\Component\Form\FormRegistry. The same exception message was thrown if either the Type class wasn't found or the Type class did not implement the Symfony\Component\Form\FormTypeInterface. This pull request adds some explaination of the reason why a type could not be found and helps application developpers to find the source of the Symfony\Component\Form\Exception\InvalidArgumentException thrown when getting a Type. Commits ------- 945f236 improve FormType::getType exception message details
2 parents 39bcfd4 + 945f236 commit 3d9676c

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/Symfony/Component/Form/FormRegistry.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,14 @@ public function getType($name)
8484

8585
if (!$type) {
8686
// Support fully-qualified class names
87-
if (class_exists($name) && in_array('Symfony\Component\Form\FormTypeInterface', class_implements($name))) {
88-
$type = new $name();
89-
} else {
90-
throw new InvalidArgumentException(sprintf('Could not load type "%s"', $name));
87+
if (!class_exists($name)) {
88+
throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not exist.', $name));
9189
}
90+
if (!is_subclass_of($name, 'Symfony\Component\Form\FormTypeInterface')) {
91+
throw new InvalidArgumentException(sprintf('Could not load type "%s": class does not implement "Symfony\Component\Form\FormTypeInterface".', $name));
92+
}
93+
94+
$type = new $name();
9295
}
9396

9497
$this->resolveAndAddType($type);

0 commit comments

Comments
 (0)
0