Fatal error when form['#tree'] = true

edgar83 - February 20, 2009 - 09:34
Project:Upload element
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

This simple test form triggers the following Fatal error on submit (make sure to upload a file...):
"Cannot use object of type stdClass as array in /var/www/drupal/includes/form.inc on line 1320"

Without the "#tree = true" it works fine.

function tst_form(&$form_state) {
  $form['#attributes']['enctype'] = 'multipart/form-data';
  $form['#tree'] = true; // remove me...

  $form['image'] = array(
    '#type'   => 'image_upload_element',
    '#title' => 'img',
    '#required' => true,
  );

  $form['submit'] = array(
   '#type' => 'submit',
   '#value' => 'Submit',
   );

  return $form;
}

#1

Alan D. - February 20, 2009 - 12:40
Priority:critical» normal

It looks like this is a possible issue with saving objects as element values in Drupal, which was the primary philosophy behind the module.

Casting the file object to an array bypasses this error in "form.inc", but breaks the module as it is expecting an object not an array.

If this is the case, then the module will have to be completely rewritten, a task that I simply do not have time to do at the moment.

I will update the intro to note this issue and leave this issue open in case some FAPI genius can come up with a simple workaround.

Otherwise it will have to wait till the next major release (6.x-2.0), as the changes will change the basic interface of the module. I am also planning multiple file support, so it is going to be a fairly major job for the next release.

#2

edgar83 - February 21, 2009 - 11:31

My solution is to put #tree = false on the upload element in my form. The form stays nested, except for this specific element.

Thanks for your great module btw :-)

#3

Alan D. - February 21, 2009 - 11:49
Version:6.x-1.1» 6.x-1.x-dev

Thanks for the feedback / workaround.

I'll leave this issue open for a future releases / versions.

#4

Electronick - March 29, 2009 - 12:13

We can use simple (object)$array and (array)$object conversion. But don't know where.

#5

Alan D. - March 30, 2009 - 08:55

The solution is not as simple as this. Casting all of the objects into arrays cascaded into another series of bugs. This should be addressed later when things get refactored a bit.

 
 

Drupal is a registered trademark of Dries Buytaert.