0

我正在尝试为其中一个模型上传照片以及何时进入编辑模式。当用户只想编辑与该记录相关的文本时,它仍然要求我上传照片。以下是我的验证规则。

    '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,
        ),
    ),

上传功能

   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;
    }
4

4 回答 4

2

uploadError 需要上传

uploadError 的代码需要上传:

public static function uploadError($check) {
    if (is_array($check) && isset($check['error'])) {
        $check = $check['error'];
    }

    return (int)$check === UPLOAD_ERR_OK;
}

如果没有上传文件,则错误将不包含该值 - 它将是UPLOAD_ERR_NO_FILE

文件上传永远不会为空

放入'allowEmpty' => true,规则定义不会产生任何影响,因为该值(如果存在)永远不会为空 - 它始终采用以下形式:

array(
    ...
    'error' => int
)

因此,最好allowEmpty从所有文件上传验证规则中删除。

使用 beforeValidate

除了处理验证规则之外,您还可以使用 beforeValidate在进行验证检查之前简单地删除文件上传数据:

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();
}

这样,display_photo如果没有要处理的上传,则完全跳过验证规则。

于 2014-06-15T11:36:03.953 回答
2

设置参数

'on' => 'create'

只是为了'uploadError'规则

'uploadError' => array(
    'rule' => array('uploadError'),
    'message' => 'Please select a Photo.',
    'on' => 'create',
),

这种方式 cake 将强制用户在创建记录时上传图像。

其他验证规则 intead 将始终有效,但前提是实际上传了文件。

手册

于 2014-06-07T08:41:36.337 回答
1

试试这个方法,我用过几次。将“型号”替换为您的型号名称。

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
}
于 2014-06-13T10:27:53.430 回答
-1

Use required = false and in your controller unset the array field that is not needed for validation based on the application logic.

于 2014-06-07T09:41:12.850 回答