2013-05-16 103 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 
    } 
}