Skipping Validation for a single file in Zend_File_Transfer
For c开发者_开发问答reating a business I upload 5 images along with a .csv file. I have used following zend validators
$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));
When I upload the CSV I get an error saying ImageSize not detected. Is there any way that I can skip ImageSize validator for .csv file?
here, example that validate first image for 435px height by 175px width and all other remaining image for 200px height by 200px width.
$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++;
}
}
To validate form write below code.
if($form->isValid($formData))
{
}
And I figured out how to do it!!
$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);
}
}
精彩评论