I just created a custom form with hook_form() for a custom node module I'm working on. My custom form includes Javascript that loads options in a second select form based on input from the first. This allows for subcategories which is nice for organization.
Problem is that I have to pass empty options to form.inc for the dynamic part of my form since they are filled in by the javascript.
If I don't pass it anything for #options it gives me a foreach error on form.inc line 496.
If I pass it empty options it tries to validate the input and won't allow the form to be completed. It can't validate input of course because it can't see my dynamic options.
Anything I can do other than edit form.inc?
If not, I propose different behavior for form.inc. I don't think it should try to loop through the #options if they don't exist. It might not seem like a bug that it errors out if #options doesn't exist, but for dynamic forms that is definitely possible.
Comments
Two problems
My experience is that there are two problems here. The first is the foreach error, and I agree that you should probably be able to send an empty array to #options. But the other problem will crop up when you try to submit a value that was created with JS and didn't exist when you built the form. The way I was able to avoid this was to build the entire form that was expected server-side, even if I didn't render all those options client-side. Don't know if there is a more recommended way. The forms API makes working with client created form elements a bit difficult (which is why it is more secure).
- Robert Douglass
-----
My Drupal book: Building Online Communities with Drupal, phpBB and WordPress
this is solved
so you set the #DANGEROUS_SKIP_CHECK attribute to TRUE and provide your own validation. And yes, it is dangerous to skip the choice checker, so make a good validation there.
and yes, an empty array() to #options is mandatory.
--
My developer blog. | The news is Now Public | Ask not what Drupal can do for you -- ask what you can do for Drupal.
--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.
this is recent I take it
Your code snippet is from cvs I assume? My 4.7 beta 4 does not have that flag:
ancient and buggy
4.7b4 is superseded by 4.7b5 -- hundreds of bugs fixed.
--
My developer blog. | The news is Now Public | Ask not what Drupal can do for you -- ask what you can do for Drupal.
--
Drupal development: making the world better, one patch at a time. | A bedroom without a teddy is like a face without a smile.
hook_form and js in 4.6??
I have a similar problem, but using 4.6. A chained selection javascript code works great as a php page, but the same code inserted in a module hook_form function do not populate #options in the form_select.
I have searched a lot, but I can't find anything useful for me.
TIA
MAYC