CakePHP Form Validation only on Entering Data

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!