在Zend_File_Transfer中跳过单个文件的验证

时间:2011-06-13 22:46:38

标签: zend-framework zend-validate zend-file

为了创建商家,我上传了5张图片和.csv文件。我使用了以下zend验证器

$upload = new Zend_File_Transfer();
        $upload->addValidator('Count', false, array('min' =>1, 'max' => 6))
               ->addValidator('Size', false, array('max' => '1Mb'))
               ->addValidator('ImageSize', false, array('minwidth' => 50,
                                                        'maxwidth' => 1000,
                                                        'minheight' => 50,
                                                        'maxheight' => 1000));

当我上传CSV时,我收到一条错误消息,指出未检测到ImageSize。有什么办法可以跳过.csv文件的ImageSize验证器吗?

2 个答案:

答案 0 :(得分:1)

此处,示例验证第一张图像的高度为435像素,高度为175像素,所有其他剩余图像为200像素高度,宽度为200像素。

$targetPath = $this->registry->DOC_ROOT.'/public/uploads/images/campersite_user_photo/';

            if(!is_dir($targetPath))
            {
                mkdir($targetPath,'0777');
            }

            $adapter->setDestination($targetPath);

            $first = true;
            $filecheck = '';

            if(isset($asAdminVal['admin_role_id']) && ($asAdminVal['admin_role_id'] == '1' || $asAdminVal['admin_role_id'] == '2'))
            {
                $photoCount = Model_TblCampersiteUserPhotos::getCamperPhotoCount($this->view->snCampId);

                if($photoCount == 0)
                {
                    $j = 1;

                    foreach ($adapter->getFileInfo() as $fields => $info) 
                    {
                        if($info['name'] != '' && $first == true)
                        {
                            $filecheck = $fields;
                        }

                        if($filecheck != '' && $first == true)
                        {
                            $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 435,'minheight' => 175,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_camper_banner_image_file_too_width_height_less'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_camper_banner_image_file_too_width_height_less'))),$fields);
                            $first = false;
                        }
                        else
                        {
                            $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 200,'minheight' => 200,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_file_too_small'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_file_too_small'))),$fields);
                        }
                        $fileInfo[$j] = $info;
                        $j++;
                    }
                }
                else
                {
                    $j = 1;
                    foreach ($adapter->getFileInfo() as $fields => $info) 
                    {
                        $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 200,'minheight' => 200,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_file_too_small'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_file_too_small'))),$fields);
                        $fileInfo[$j] = $info;
                        $j++;
                    }   
                }
            }
            else
            {
                $j = 1;
                foreach ($adapter->getFileInfo() as $fields => $info) 
                {
                    $form->photo_path->addValidator('ImageSize', false,array('minwidth' => 200,'minheight' => 200,'messages' => array('fileImageSizeWidthTooSmall' => $this->translate->_('msg_file_too_small'),'fileImageSizeHeightTooSmall' => $this->translate->_('msg_file_too_small'))),$fields);
                    $fileInfo[$j] = $info;
                    $j++;                   
                }
            }

验证表格下面的代码。

if($form->isValid($formData))
{

}

答案 1 :(得分:0)

我想出了怎么做!!

$upload = new Zend_File_Transfer();
    $files = $upload->getFileInfo();
    foreach ($files as $fields => $contents)
    {
        if ($fields!= 'filename6')  -- Skipping the validation for csv file
        {
            $upload->addValidator('Count', false, array('min' =>1, 'max' => 6),$fields)
                   ->addValidator('Size', false, array('max' => '1Mb'),$fields)
                   ->addValidator('ImageSize', false, array('minwidth' => 50,
                                                            'maxwidth' => 1000,
                                                            'minheight' => 50,
                                                            'maxheight' => 1000),$fields);
        }
        elseif ($fields == 'filename6') -- To validate only csv file
        {
            $upload->addValidator('Extension', false, 'csv', $fields)
                   ->addValidator('Size', false, array('max' => '1Mb'),$fields);
        }
    }