【问题标题】:Cakephp Uploader validation error with jQuery Form plugin使用 jQuery 表单插件的 Cakephp Uploader 验证错误
【发布时间】:2014-05-16 15:49:30
【问题描述】:

我正在使用 Miles Johnson file Uploader 和我的 CakePHP。每次对我来说都很好。但是今天我想以我的观点使用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',
            )
        )
    )
);

如有任何建议,我将不胜感激。

【问题讨论】:

  • it could not catch required validation 到底是什么意思?
  • @FazalRasel 如果我不选择任何文件,它不会显示错误

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


【解决方案1】:

好吧,那就试试这个-

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

【讨论】:

  • 已经尝试过了。但它不起作用。更新案例也显示错误。
  • 并且还显示存在的另一个错误的错误。如果为空文件则显示文件扩展名丢失错误。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2013-06-11
    • 2019-09-10
    相关资源
    最近更新 更多