If a form has "file upload" as the only component in the fieldset, the "file upload" component is never processed. Adding a hidden component (or any other form processed component) to the fieldset is a workaround.

Some exploratory debugging shows that $form_values or $_POST does not contain the file component in webform_client_form_validate().

Drupal 5.7/webform 5.x-2.0.

Comments

quicksketch’s picture

Assigned: dharmatech » Unassigned

Thanks, this sounds very possible due to the way that file uploads are handled. HTML handles file elements differently from any other POST data, not even including any information about the file in the $_POST data. Instead, all information is found in the $_FILES array. Then I'm not sure if it's PHP's handling of files or Drupal's, but the actual location of the file component in the form is lost, so Webform does it's best to put the file back into the correct location in the form.

I'll get to this eventually, but if any savvy dev wants to take a stab at correcting this I'll gladly take patches. Thanks for the report.

dharmatech’s picture

Status: Active » Needs review
StatusFileSize
new1.97 KB

The following patch appears to fix the problem with lone file upload components in a fieldset. I tested using file components inside nested fieldsets with and without additional components. I'm sure I didn't test every possible scenario, but it seems to work for me.

Took a while to figure out that webform_get_cid() was returning a null $cid because it was exhausting it's foreach without increasing the $pid.

quicksketch’s picture

Status: Needs review » Needs work

dharmatech, thanks for the patch! Unfortunately, this doesn't apply at all to the latest 2.x version. Could you download the development version or checkout the latest copy of the DRUPAL-5--2 branch from CVS?

dharmatech’s picture

Status: Needs work » Needs review
StatusFileSize
new2.08 KB

Sorry, I thought I was working with the latest 2.x release. Here's the patch for 5.x-2.x-dev.

quicksketch’s picture

Status: Needs review » Needs work

This patch looks like it would introduce several problems with some complex Webforms. When iterating through fieldsets and setting the values to empty arrays, this only gets the first level of fieldsets, not nested fieldsets (Webform does most operations recursively). Does this affect the functionality of the patch?

Also, the removal of $local_value['pid'] == $cid from the conditional will break webforms where the same formkey is used in different fieldsets, say a situation like this:

Mailing address [mailing_address fieldset]
- street
- town
- state
- zip

Shipping address [shipping_address fieldset]
- street
- town
- state
- zip

Without that conditional, the values from the second fieldset will not be used, because Webform will find the street, town, state, and zip keys from the first fieldset and use them instead.

dharmatech’s picture

As for initializing the fieldsets as empty arrays, the foreach should take care of it shouldn't it? In my testing, I've found that my fix works for nested fieldsets as stated previously.

I thought the formkey was unique for the webform... I guess it's not. I'll work out a fix.

quicksketch’s picture

As for initializing the fieldsets as empty arrays, the foreach should take care of it shouldn't it?

I'm not sure, if you had a fieldset inside a fieldset, the inner fieldset would be located at $form_values['submitted'][$parent_component['form_key']][$component['form_key']]. So, it doesn't look like it would be initialized to an empty array. I'll take a further look at the next patch.

dharmatech’s picture

I think I discovered another bug in the process of working on this patch. Using the latest, clean 5.x-2.x-dev code, without my patch applied.

1. Create a form with two fieldsets.
2. Inside each fieldset add a file component and give each file component the same form key.
3. View the form and upload two different files.

It seems to stem from file.inc _webform_submit_file(). print_r($_FILES) shows two arrays that are identical.

I might be way off base here so please correct me if I am, but a better approach might be to enforce unique form keys per form. It would greatly simplify things and reduce the risk of clobbering.

quicksketch’s picture

We should definitely add more validation that checks that file keys are unique, PHP doesn't provide us with the nice array structure that we get for $_POST in $_FILES, so they do indeed need to be unique.

Anyway, yes, that's another bug, but let's handle it in another issue.

dharmatech’s picture

Status: Needs work » Needs review
StatusFileSize
new2.14 KB

With the above bug related to duplicate form keys aside, I'm attaching a patch that addresses the original problem of file upload not working when a file component is the only child of a fieldset. This patch also addresses your two concerns related to initializing the empty fieldset array and correctly handling more complex forms where the form key is the same.

dharmatech’s picture

Just wanted to make sure this hasn't fallen through the cracks. Since it's a week old, do I need to generate a new patch?

quicksketch’s picture

Sorry I just haven't had a chance to review yet. I'm booked up but I'll try to have a look soon. There are several other fixes that necessitate rolling out a 2.1 version, I'll makes sure that we figure this one out before then.

dharmatech’s picture

I guess this didn't make it into the 2.1 release, eh. Do you want me to generate a new patch against 2.1 or dev?

quicksketch’s picture

Yeah, sorry I didn't include this, I was afraid of potential problems it could cause and I'll need to test it a bit more extensively. If you reroll against 2.x dev that would be great.

dharmatech’s picture

Version: 5.x-2.0 » 5.x-2.1
StatusFileSize
new2.14 KB

Here's the latest patch...

dharmatech’s picture

StatusFileSize
new2.14 KB

Here's the latest patch...

dharmatech’s picture

Need another diff?

quicksketch’s picture

This patch works in my testing (thanks for putting up with my slow response so far). Could we try to take an alternate approach with this patch though? There are frequent complaints of file components not working in multipage forms, lack of an ajax upload, and failing when a single required field is missing. All of these solutions are going to be handled separately, but all of them are going to require... a companion hidden field (probably containing the file path of the upload file in Drupal 5, or the File ID in Drupal 6). If we add a companion hidden field to all file upload right now (just leave the value empty), this would solve our empty fieldset problem and set us up for future fixes.

dharmatech’s picture

Yeah, I watch the issue tracker frequently and I've noticed all the file upload related bugs/requests. I've also noticed how dominant the D6 bugs are over D5. Unfortunately, we're still developing for D5 so I won't be much help. If there's something you want me to do w/this I'll give it a shot. Sounds like you're happy with the workaround of adding a hidden field until a bette r solution is worked out.

Should we change the bug status to postponed?

rootwork’s picture

I just wanted to note that this bug also affects the 6.x branch (there doesn't seem to be any way to mark a bug as applying to more than one version). The workaround seems simple enough, but it might be worth including something about it in the readme or the project page if it's not able to be fixed soon -- I was searching for this bug in the 6.x issue queue and only found this page with the workaround when I thought to search all versions.

quicksketch’s picture

StatusFileSize
new5.28 KB
new5.13 KB

I got around to looking at this again finally. I think this new solution rmakes code more efficient and removes a few hacks. It should also theoretically make it easier to handle uploads in multi-page forms, which would be nice to eventually support.

Rather than adding in more loops, this approach makes such loops unnecessary by providing some information in the $_POST array where the file will ultimately be positioned. A hidden field is output in addition to the file field, then the values from the saved file inserted into the hidden fields location when running the processing. This makes it so that the file data stays in the same place as before, and we eliminate all the guesswork in trying to find where files should belong.

Drupal 5 and 6 patches attached.

quicksketch’s picture

Status: Needs review » Fixed

I went ahead and committed these patches. Please reopen if these caused new problems or still experience file uploads not being saved when the file element is in a fieldset by itself.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.