可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to Upload a photo for one of the models and when i am going to the edit mode. It still asks me to upload the photo when the user only wants to edit the text related to that record. Below is my Validation Rules.
'display_photo' => array( 'uploadError' => array( 'rule' => array('uploadError'), 'message' => 'Please select a Photo.', 'allowEmpty' => true, ), 'mimeType' => array( 'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')), 'message' => 'Please only upload images (gif, png, jpg).', 'allowEmpty' => true, ), 'fileSize' => array( 'rule' => array('fileSize', '<=', '5MB'), 'message' => 'Photo must be less than 5MB.', 'allowEmpty' => true, ), 'photoUpload' => array( 'rule' => array('photoUpload'), 'message' => 'Unable to process Photo upload.', 'allowEmpty' => true, ), ),
Upload Function
public function photoUpload($check = array()) { if (!is_uploaded_file($check['display_photo']['tmp_name'])) { return false; } if (!move_uploaded_file($check['display_photo']['tmp_name'], WWW_ROOT . 'img' . DS . 'portfolios' . DS . $this->data[$this->alias]['slug'].".".pathinfo($check['display_photo']['name'], PATHINFO_EXTENSION))) { return false; } $this->data[$this->alias]['display_photo'] = $this->data[$this->alias]['slug'].".".pathinfo($check['display_photo']['name'], PATHINFO_EXTENSION); return true; }
回答1:
set the parameter
'on' => 'create'
just for the 'uploadError'
rule
'uploadError' => array( 'rule' => array('uploadError'), 'message' => 'Please select a Photo.', 'on' => 'create', ),
This way cake will force the user to upload an image just when the record is created.
The other validation rules, intead, will always be valid, but only if a file is actually uploaded.
see the manual
回答2:
uploadError expects an upload
The code for uploadError expects an upload:
public static function uploadError($check) { if (is_array($check) && isset($check['error'])) { $check = $check['error']; } return (int)$check === UPLOAD_ERR_OK; }
If there is no file uploaded the error will not contain that value - it'll be UPLOAD_ERR_NO_FILE.
File uploads are never empty
Putting 'allowEmpty' => true,
in the rule definition won't have any effect as the value, if present, will never be empty - it's always of the form:
array( ... 'error' => int )
As such it's best to remove allowEmpty
from all the file-upload validation rules.
Use beforeValidate
Instead of dealing with the validation rules, you can instead use beforeValidate to simply remove the file upload data before the validation check is made:
public function beforeValidate() { if ( isset($this->data[$this->alias]['display_photo']) && $this->data[$this->alias]['display_photo']['error'] === UPLOAD_ERR_NO_FILE ) { unset($this->data[$this->alias['display_photo']); } return parent::beforeValidate(); }
In this way the validation rules for display_photo
are skipped entirely if there is no upload to process.
回答3:
Try this approach, I used this several times. Replace 'Model' with your model name.
if($this->request->data['Molel']['display_photo']['name']!=''){ // put your code here for upload image else { unset($this->request->data['Model']['display_photo']); // this will exclude this from validation }
回答4:
Use required = false
and in your controller unset the array field that is not needed for validation based on the application logic.