I have a form with a simple textfield where the user enters a string. I want to check if this string is a valid form_id in the website. I thought about using the drupal_get_form but even if it would work, it would be kind of an overkill to retrieve the whole form array just to check if it exists.

Any ideas are more than welcome.

  • Could you not use Devel's dpm($form)? – WestieUK Mar 26 '12 at 14:45
  • I need to determine it programmatically. Which means that when the user presses the submit button of the form, I need to determine whether the user submission in the textfield input is actually a valid form_id or not. And the module will be working in the final website so no devel module will be enabled. – user6102 Mar 26 '12 at 14:52
  • Forgive me but I am not quite following, can you give a little more context? To answer the programmatic part, you can do this on submit by adding a hook_form_alter and add a new submit handler. I dont know why you need this to show to users? You could just use the dpm($form) for debugging unless I am missing something? – WestieUK Mar 26 '12 at 15:08
  • Basically, the user has a textfield in the module configuration. In this textfield he needs to enter a valid form_id. After he enters the form_id, I will do some theming and altering to the form but first, I need to figure if the form_id that he entered is actually valid (so that I can return a proper error message and not a PHP error: argument not valid) – user6102 Mar 26 '12 at 16:37

Since the 'ID' you pass to drupal_get_form() is actually the name of a PHP function, the simplest way to see if it exists is using plain old PHP:

if (function_exists('form_id')) {
  // ...

If you need to check that the return from the function is a suitable Drupal form array, you'll need to use one of the methods described by MPD.

This will only work for forms for which the name is explicitly defined as the string you're passing in; i.e. forms whose root function is provided by a hook_forms() implementation will not match using this method.

It's worth bearing in mind that with whichever method you use, any form you're trying to identify will have to exist in a file that's included in the current page request. With that in mind it might be wise to keep all the form functions in question in the .module file of an enabled module, which will always be included.


When tasked with this like this, your best place to start is the API function that you know about, and start working backwards though the code until you find what you need.

Take a look at the code for drupal_retrieve_form, which gets called from drupal_build_form, which gets called from drupal_get_form. You should be able to clone/modify that for your needs.

Personally, though, I suspect I would just call drupal_retrieve_form and see if I get back something valid.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy