2013-05-16 134 views
2

即时尝试验证cakephp模型中的文件字段与创建有效的扩展和更新尝试验证文件只有字段不empty.On创建验证工作正常,但在更新它验证if场empty.I要验证扩展,只有当字段不为空 这里是在模型验证阵列我的验证规则cakephp文件字​​段验证

'image' => array(
     'rule1'=>array(
      'rule' => array('extension',array('jpeg','jpg','png','gif')), 
      'required' => 'create', 
      'allowEmpty' => true, 
      'message' => 'Select Valid Image', 
      'on' => 'create', 
      'last'=>true 
     ), 
     'rule2'=>array(
      'rule' => array('extension',array('jpeg','jpg','png','gif')), 
      //'required' => 'create', 
      'allowEmpty' => true, 
      'message' => 'Select Valid Image', 
      'on' => 'update', 
     ), 
    ), 
+0

你在使用插件吗?如果是这样,请在您的问题中包含该信息。如果没有,请考虑使用一个,如https://github.com/josegonzalez/upload –

+0

不,我没有使用任何插件,但我使用模型beforeSave方法来上传和处理文件 – Vimal

回答

1

我们可以通过自定义验证如下

public $validate =array(
    'image' => array(
     'rule' => array('checkValidImage'), 
     ) 
    ); 


public function checkValidImage($field) 
     { 

     $extension = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/x-png', 'image/png', 'image/jpg'); 
     $isValidFile = in_array($field['new_image']['type'], $extension); 
     $errors = array(); 
     $editMethod = false; 
     if(!empty($this->data['ModelName']['id'])) // It will work for Update Method 
     { 
      if(!empty($this->data['ModelName']['image']['name'])) 
      { 
      if (($field['image']['error'] == 1)) 
      { 
       $errors [] = "Please upload jpg,png or gif files with size 2 MB."; 
      } 
      else if (empty($field['image']['name'])) 
      { 
       $errors [] = "Please upload image"; 
      } 
      else if ($field['image']['size'] >= 2097152) { 
       $errors [] = "Please upload jpg,png or gif files with size 2 MB."; 
      } 
      else if ($isValidFile !=1) 
      { 

       $errors [] = "Please select file in gif,jpeg,png format."; 
      } 

      }else 
       { 
        $errors [] = "Please select file in gif,jpeg,png format."; 
       } 

     } 
     else 
     { 
      if(!empty($this->data['ModelName']['image']['name'])) // It will work for Create Method 
      { 
       if (($field['image']['error'] == 1)) 
       { 
        $errors [] = "Please upload jpg,png or gif files with size 2 MB."; 
       } 
       else if (empty($field['ModelName']['name'])) 
       { 
        $errors [] = "Please upload image"; 
       } 
       else if ($field['ModelName']['size'] >= 2097152) { 
        $errors [] = "Please upload jpg,png or gif files with size 2 MB."; 
       } 
       else if (!(in_array($field['image']['type'], $extension))) 
       { 
        $errors [] = "Please select file in gif,jpeg,png format."; 
       } 

      } 



     } 

     if (!empty($errors)) 
     { 
      return implode("\n", $errors); 
     } 
     return true; 
    } 
4

这里做是验证ima的正确方法与上创建,并且可允许使图像场

图片字段验证阵列

'image' => array(
    'rule1'=>array(
     'rule' => array('extension',array('jpeg','jpg','png','gif')), 
     'required' => 'create', 
     'allowEmpty' => true, 
     'message' => 'Select Valid Image', 
     'on' => 'create', 
     'last'=>true 
    ), 
    'rule2'=>array(
     'rule' => array('extension',array('jpeg','jpg','png','gif')), 
     'message' => 'Select Valid Image', 
     'on' => 'update', 
    ), 
), 

和取消像场的更新空在beforevalidation在更新动作所需GE字段

function beforeValidate($options = array()){ 
    if(empty($this->data[$this->alias]['id'])) 
    { 
     return true; 
    } 
    else 
    { 
     if(empty($this->data[$this->alias]["image"]["name"])){ 
     unset($this->data[$this->alias]["image"]); 
     } 
     return true; //this is required, otherwise validation will always fail 
    } 
}