Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top