CakePHP 表单验证仅在输入数据时 [英] CakePHP Form Validation only on Entering Data

查看:17
本文介绍了CakePHP 表单验证仅在输入数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

上传功能

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

推荐答案

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.

If there is no file uploaded the error will not contain that value - it'll be UPLOAD_ERR_NO_FILE.

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

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
)

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

As such it's best to remove allowEmpty from all the file-upload validation rules.

不是处理验证规则,而是使用 beforeValidate 来简单地删除文件上传数据 before 进行验证检查:

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

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

In this way the validation rules for display_photo are skipped entirely if there is no upload to process.

这篇关于CakePHP 表单验证仅在输入数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆