使用jQuery Form插件的Cakephp Uploader验证错误

时间:2014-04-04 11:02:08

标签: jquery validation cakephp file-upload jquery-plugins

我在CakePHP上使用Miles Johnson file Uploader。它每次对我来说都很好。但今天我想与jQuery Form Plugin一起使用我的观点,但它无法捕获必需的验证。没有jQuery Form Plugin它的工作正常。我的jQuery代码如下:

jQuery('#ImageAddForm').ajaxForm({
    iframe:false,
    beforeSend: function() {},
    uploadProgress: function(event, position, total, percentComplete) {},
    success: function() {},
    complete: function(xhr) {}
});

还有模型验证码,如下所示:

public $actsAs = array(
    'Uploader.Attachment' => array(
        'imagefile' => array(
            'finalPath'=>'/files/uploads/'      
        )
    ),
    'Uploader.FileValidation' => array(
        'imagefile' => array(               
            'extension' => array('pdf','zip'),
            'mimeType' => array('application/pdf','application/zip'),
            'filesize' => array(
                'value'=>104857600,
                'error'=>'Your file size is too large; maximum size 100 MB'
            ),
            'required' =>  array(
                'value' => true,
                'on' => 'create',
                'error' => 'Please choose iPad Portrait file',
            )
        )
    )
);

任何建议都会对我表示感激。

2 个答案:

答案 0 :(得分:0)

那么,试试这个 -

    'required' =>  array(
        'value' => true,
        'on' => 'create',
        'allowEmtpy' => false,
        'error' => 'Please choose iPad Portrait file',
    )

答案 1 :(得分:0)

我已经使用我的模型中的以下代码解决了这个问题:

function beforeValidate($options=array()) {
    $emptyfile=array('name' => '','type' => '','tmp_name' => '','error' => (int) 4,'size' => (int) 0);
    if(empty($this->data['Image']['imagefile'])){
        $this->data['Image']['imagefile']=$emptyfile;
    }   
    return true;
}