0

I used to have a ZF controller that was processing a fineuploader ajax upload. The code was simple:

$adapter = new Zend_File_Transfer_Adapter_Http();
        $filename = uniqid();
        $adapter->addFilter('Rename', APPLICATION_PATH . "/../public/temp-images/" . $filename);
        $adapter->addValidator('Size', false, array("max" => "2MB"));
        $adapter->addValidator('isImage', false);
        if ($adapter->receive()) {
            // Get mime type
            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $mimeType = finfo_file($finfo, APPLICATION_PATH . "/../public/temp-images/" . $filename);
            finfo_close($finfo);

            preg_match('/(.*)\/(.*)/', $mimeType, $matches);
            $extension = '.' . $matches[2];

Now I'm refactoring using Symfony2 and I have difficulties doing the same thing. This is what I have so far:

$form = $this->createFormBuilder()
    ->add('qqfile', 'file', array('constraints' => new File(array('maxSize' => '2M'))))
    ->getForm();


    if ($form->isValid()) {
       die('yes');
    } else {
       die('no');
    }

This is what gets sent from the browser:

------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qquuid"

b977c4b2-0edb-486b-aa86-4558275598aa ------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qqtotalfilesize"

14092 ------WebKitFormBoundaryYPzt2RqJ6W4awSFp Content-Disposition: form-data; name="qqfile"; filename="ae35e28.png" Content-Type: image/png

------WebKitFormBoundaryYPzt2RqJ6W4awSFp--

Now, I know for sure that the form will not be validated, because the POSTed data contains no name for the form. Actually, I don't even need to validate the whole form, just the uploaded file (like here Symfony2: upload a file using a file upload plugin), but how do I use validation for it?

Community
  • 1
  • 1
Zorrocaesar
  • 748
  • 1
  • 7
  • 22
  • How do you send the ajax request for fileupload? Does Zend_File_Transfer_Adapter_Http add js code for that? As far as I know Symfony doesn't add any js code and you have to handle that part. – MKoosej Jul 30 '14 at 17:48
  • The js part is generated independent of Symfony or any other PHP code. But I think you can ignore that. My problem is how to validate this form once it is being submitted. – Zorrocaesar Jul 31 '14 at 06:43

1 Answers1

0

I eventually figured it out myself.

Instead of using a form without a class, I created a class form whose getName() method returns an empty string. I set mapped=false for all the other fields except qqfile and also disabled the csrf protection for the form. In this way, the form gets properly submitted and the file input validated.

Zorrocaesar
  • 748
  • 1
  • 7
  • 22