【问题标题】:CakePHP Model ValidationCakePHP 模型验证
【发布时间】:2014-06-06 13:18:39
【问题描述】:

我在 CakePHP 中有一个简单的表单。

我想上传图片并想在recent_picture 上定义CakePHP 验证。

<?php echo $this->Form->create('MyController', array('type' => 'file')); ?>
<?php echo $this->Form->file('recent_picture'); ?>
<?php echo $this->Form->end(__('Create Account & Continue')); ?>

我已经在模型中定义了验证。

     public $validate = array(
        'recent_picture' => array(
            'rule'    => array(
            'extension',
            array('gif', 'jpeg', 'png', 'jpg')
            ),
            'message' => 'Please supply a valid image.'
        ),  
    );

上述验证有效,如果我没有填写它会给出错误。但它没有在图像扩展上验证。

所以请帮帮我。

我们将不胜感激。

谢谢。

【问题讨论】:

标签: php validation cakephp


【解决方案1】:

我不知道为什么它不起作用,因为它对我也不起作用(我可能关注了像你这样的文档),就在一两天前,为了挣扎,我选择了一种方式。也许这不是最好的答案,也不是干净的答案,但您可以在上传后使用类似这样的内容验证扩展:

$mime_type_correct = preg_match("/image\/*\w+/", $file_data['type']);

希望对您有所帮助。 PS:这个sn-p也可能对你有用:

        switch ($file_data['error']) {
        case 0:
            //no error
            break;
        case 1:
            $err_msg = __('The uploaded file exceeds the upload_max_filesize');
            $error = true;
            break;
        case 2:
            $err_msg = __('The uploaded file exceeds the MAX_FILE_SIZE from form');
            $error = true;
            break;
        case 3:
            $error_msg = __('The uploaded file was only partially uploaded.');
            $error = true;
            break;
        case 4:
            $err_msg = __('No file was uploaded.');
            $error = true;
            break;
        case 6:
            $err_msg = __('Missing a temporary folder.');
            $error = true;
            break;
        case 7:
            $err_msg = __('Failed to write file to disk.');
            $error = true;
            break;
        case 8:
            $err_msg = __('A PHP extension stopped the file upload.');
            $error = true;
            break;

        default:
            break;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多