Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem
- Error message displayed after writing code for a form:
InvalidArgumentException: The form argument Drupal\mymodule\Form\MymoduleLoginForm is not a valid form. in Drupal\Core\Form\FormBuilder->getFormId() (line 190 of core/lib/Drupal/Core/Form/FormBuilder.php)
Cause
- Class name did not match filename, so the form could not be loaded.
Details
- Error message suggests that something WITHIN the form is not valid, so I'm trying to spot an error in the completely wrong place.
Proposed solution
- Change error message to state clearly that the system wasn't able to locate / load the form.
Comments
Comment #2
sunComment #3
sunComment #5
morsokAh just got bitten by that, looking everywhere when It was a simple typo (missing uppercase in the class name).
Comment #6
jhedstromThis splits the exception into 2, for the 2 different potential causes.
Comment #7
jhedstromAlthough, I wonder if this is an appropriate place to use `assert()` instead?
Comment #9
jhedstromThis updates the test, and adds coverage for the new logic and exception text.
Comment #11
kkalashnikov CreditAttribution: kkalashnikov at Srijan | A Material+ Company for Drupal India Association commentedComment #12
kkalashnikov CreditAttribution: kkalashnikov at Srijan | A Material+ Company for Drupal India Association commentedPatch for Drupal 8.8.x
Comment #17
dimitriskr CreditAttribution: dimitriskr commentedThis patch helped a lot
I uploaded a new version of the last patch where it adds a space after an elseif()
Comment #18
DanielVezaDoes this need to be an elseif?
I also feel like the patch in #9 by @jhedstrom had more test coverage thats missing from this patch. I think that needs to be added back in.
Comment #19
LendudeI think we use ::class now and not get_class()
Comment #20
dimitriskr CreditAttribution: dimitriskr commentedHere's the new patch with the suggested changes. I won't enable tests until you confirm the changes are the correct
@DanielVeza the elseif must be elseif because the
if (!is_object($form_arg)) {
checks $form_arg is object, but then we have to make sure it's instanceof FormInterface because here$form_state->setFormObject($form_arg);
it expects an object of FormInterfaceComment #21
LendudeSounds like a good addition, tests cover the new exception. Looks good.
Comment #23
catchCommitted/pushed to 10.1.x, cherry-picked to 10.0.x and 9.5.x, thanks!
Comment #24
longwaveThis has broken compatibility with PHP 7.3 in 9.5.x, technically we don't support it any more but this currently causes so many errors DrupalCI cannot report them: https://www.drupal.org/pift-ci-job/2509655
The error is
Comment #25
dimitriskr CreditAttribution: dimitriskr commentedAh interesting
So shall we use
get_class()
instead?Comment #26
longwaveI think get_class() would work fine.
Comment #27
dimitriskr CreditAttribution: dimitriskr commentedComment #29
catchReverted from 9.5.x, leaving needs work in case we want to backport a 7.3-friendly version, but we could also just leave this in 10.0.x
Comment #30
dimitriskr CreditAttribution: dimitriskr commentedHere's the patch again, using
get_class()
instead of::class
to support PHP 7.3 and 7.4Comment #31
longwaveLooks good, thank you.
Comment #32
alexpottCommitted 83d2b0b and pushed to 9.5.x. Thanks!