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 |
Jump to:
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
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
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
Thanks for the feedback / workaround.
I'll leave this issue open for a future releases / versions.
#4
We can use simple (object)$array and (array)$object conversion. But don't know where.
#5
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.