Monday, September 27, 2010

MeioUpload + hasMany + JavaScript-Generated Fields

I'm using CakePHP 1.3.3/MeioUpload 2.1.1/PHP 5.3.1. I've got a very
standard hasMany/belongsTo set of models. On the many side is a simple
table with id, filename, mimetype, and dir using actsAs MeioUpload. I
know that I will always have at least one file on the many side so, in
my add.ctp for the master side, I've got one set of fields for a
single record for the many side (name="data[ItemsFile][0][filename]"
and id="ItemsFile0Filename", and so on for the other fields).

When I add a record using that view, it works perfectly, uploading the
associated file and making appropriate entries into the database. (My
tremendous praise to Jose Gonzalez!)

Now, comes the problem. I know that I'll need a varying number of
records on the many side, so I add a JQuery button to generate an
additional set of fields for an additional file upload
(name="data[ItemsFile][1][filename]" and id="ItemsFile1Filename" and
so on for the other fields). When I try to add a record using the
original, hardcoded set of fields and the generated set of fields to
upload two files, it uploads the file entered in the original,
hardcoded set of fields, but not the file entered in the generated set
of fields.

I then use debug() to look at what data is being sent to the
controller from add.ctp. Lo and behold, only the data from the first,
hardcoded set of upload fields is in the array.

Next step, I look at the source -- after clicking the button to
generate a second set of fields -- using Firebug > View Source > View
Generated Source. The code looks perfect, but just to make sure, I
copy the jQuery-generated HTML from the Firebug Generated Source and
paste it into add.ctp below the original hardcoded set of upload
fields.

When I test that out, guess what happens! It uploads the two files
with no problem at all and makes the appropriate entries in the
database! This is the very same HTML code cut-and-pasted into add.ctp
that wouldn't work when it was jQuery-generated!

Can anybody give me any idea what I'm missing? This one has me
completely stumped.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: