The Case of the Drupal Form that Would Not Post

This is the story of a Drupal form that would not handle data submissions correctly when a large number of fields were present.

Working on a Drupal site, I came across the strange case of a settings form that would not save its values when a large number of fields were present. The form helps site admins associate certain vehicle model numbers to their appropriate model name, class, and group. A class is basically a collection of similar models, and a group is a collection of similar body types such as sedan, coupe, or SUV. We have many model numbers, most of which have no actual inventory on existence, so we don’t really display those in the settings form, but yesterday we found a bug that requires us to display all model numbers. When all model numbers are being displayed in the form, the form stops working properly, and no submission is ever saved.

The form submission process in Drupal is rather complex, but all you need to know at this point is that Drupal sets an element process_input in the $form_state array that it passes around the functions that handle form processing. If process_input is set to true, then the form submission is handled, otherwise it is not. This happens in the form_builder function defined in includes/form.inc. In order for process_input to be set to truethe form must either be a programmed form, or the $form_state['input']['form_id'] variable must be defined and equal to the $form_id variable that Drupal passes around the form processing functions.

In my case the form was failing to process because the $form_state['input']['form_id'] variable was set to null. At this point, you need to know that $form_state['input'] is set to the value of $_POST for forms submitted using the POST method. Looking at both, the form, and the parameters it sent in the request, I could see that there was a form_id input, and that it was being sent with a value equal to $form_id but PHP had no knowledge of it. It turns out the request wasn’t being fully caught by PHP.

At this point, my guess is that the request is too big for my server settings, or there are too many fields being sent. PHP has settings for both, the maximum size of a POST request that it handles, and the maximum number of input variables. As far as I know there are two places were you can modify these settings: the php.ini file, and the .htaccess file. The preferred way would be to modify the php.ini file, but depending on your host you may not have access to that. In that case, using an .htaccess file would be your best option.

In this stackoverflow question you can get an idea of how to go about modifying the values in case you ever encounter the same issue.