文件上传的CakePHP验证

文件上传的CakePHP验证,php,validation,cakephp,file-upload,Php,Validation,Cakephp,File Upload,我试图在文件上传时使用cake 2.3.8进行验证,以确保只能上传PDF。我把这个完全建立在教程的基础上 我的表单在输入旁边显示星号,当我从模型中删除验证时,星号就会消失。我假设这意味着它“看到”了用于验证的输入,但我就是不明白为什么连自定义验证都没有被触发 这是表格 echo $this->Form->create('Upload', array('type' => 'file')); echo $this->Form->input('file_upload',

我试图在文件上传时使用cake 2.3.8进行验证,以确保只能上传PDF。我把这个完全建立在教程的基础上

我的表单在输入旁边显示星号,当我从模型中删除验证时,星号就会消失。我假设这意味着它“看到”了用于验证的输入,但我就是不明白为什么连自定义验证都没有被触发

这是表格

echo $this->Form->create('Upload', array('type' => 'file'));
echo $this->Form->input('file_upload', array('type' => 'file'));
echo $this->Form->input('file_title');
echo $this->Form->end(__('Upload File!', true));
echo "<pre>";
print_r(sth);
echo "</pre>";`
这是我上传模型中的代码

public function checkUpload(){
    echo "test";   //check to see if it reaches this...not displaying
    return false;  //the error message should be set just for testing, it's not displaying though
}


public $validate = array(
    'file_upload' => array(
        'extension' => array(
            'rule' => array('extension', array('pdf')),
             'message' => 'Only pdf files',
         ),
         'upload-file' => array(
                 'rule' => array('checkUpload'),
                 'message' => 'Error uploading file'
          )
    )
);

如果您试图调试蛋糕中的某个东西,请始终使用
debug(sth)//sth可以是变量,也可以是字符串,因为蛋糕中的调试意味着

echo”“;
印刷(某物);
回声“`
它的格式已经很好了。
然后在那之后,你必须把
die()
放进去,否则在echo sth之后,它会加载视图,这就是为什么即使有输出,你也看不到它的原因。

下面是我的答案(尽管是针对
cakephp 1.3
):

模型中
将以下
验证
添加到
$validate
变量中

<!-- The classes are for twitter bootstrap 3 - replace with your own -->
<?= $form->error('pdf_file', null, array('class' => 'text-danger help-block'));?>
您可能需要自己显示错误验证消息,您可以使用以下方法执行此操作:



什么是某物?我试过了,上面写着“使用未定义的常量sth-假定的‘sth’”,哈哈……sth意味着你要调试的东西……在这种情况下,你可以调试(“这里”);出现此错误意味着您已经输入了checkUploadI函数。我通过手动设置数据并调用validates使其工作。我不知道为什么它以前没有打电话,特别是因为星号让我相信它是。
<!-- The classes are for twitter bootstrap 3 - replace with your own -->
<?= $form->error('pdf_file', null, array('class' => 'text-danger help-block'));?>