Php Dropzone Laravel表单验证错误?

Php Dropzone Laravel表单验证错误?,php,jquery,forms,validation,laravel-5,Php,Jquery,Forms,Validation,Laravel 5,问题:图像上的表单后期验证失败。 错误:需要图像 在我的验证规则中,我设置了: 'images' => 'required|image', 在我的表格中,我构建了以下内容: <div class="panel panel-default"> <div class="panel-heading">Add photos</div> <div class="panel-body"> <div id="m

问题:图像上的表单后期验证失败。 错误:需要图像

在我的验证规则中,我设置了:

'images' => 'required|image',
在我的表格中,我构建了以下内容:

<div class="panel panel-default">

    <div class="panel-heading">Add photos</div>

    <div class="panel-body">

        <div id="myDropzone" class="dropzone">

        </div>

    </div>

</div>
我发布表单的Ajax请求如下:

var form = $(this);

var formdata = new FormData(form[0]);

form.find('div.alert').remove();

e.preventDefault();

var action = $(this).attr('action');

var method = $('input[name=_method]').val() || 'POST';

$.ajax({
    type: method,
    action: action,
    data: formdata,
    processData: false,
    contentType: false,
    dataType: 'json',
    success: function(data) {
        console.log(data);
    },
    error: function(data) {
        var errors = data.responseJSON;

        $.each(errors, function(key, value) {
            console.log(key + ' - ' + value);
            $('#' + key).after('<div class="alert alert-danger">' + value + '</div>');
        });

    }

});
var form=$(这是);
var formdata=新formdata(表格[0]);
form.find('div.alert').remove();
e、 预防默认值();
var action=$(this.attr('action');
var method=$('input[name=_method]')。val()| |'POST';
$.ajax({
类型:方法,
行动:行动,
数据:formdata,
processData:false,
contentType:false,
数据类型:“json”,
成功:功能(数据){
控制台日志(数据);
},
错误:函数(数据){
var errors=data.responseJSON;
$.each(错误、函数(键、值){
console.log(键+'-'+值);
$('#'+键)。在(''+value+'')之后;
});
}
});

感谢阅读

Dropzone将文件字段默认设置为“文件”

您可以将.dropzone()函数修改为:

Dropzone.autoDiscover = false;

$('#myDropzone').dropzone({
    url: 'dashboard/add/products',
    paramName: "images",
    uploadMultiple: true,
    maxFiles: 10,
    acceptedFiles: '.jpg, .jpeg',
    autoProcessQueue: false, // myDropzone.processQueue() to upload dropped files
    addRemoveLinks: true,
    dictRemoveFile: "Remove image"
});

您可以在此处浏览他们的文档:

Dropzone将文件字段默认设置为“file”

您可以将.dropzone()函数修改为:

Dropzone.autoDiscover = false;

$('#myDropzone').dropzone({
    url: 'dashboard/add/products',
    paramName: "images",
    uploadMultiple: true,
    maxFiles: 10,
    acceptedFiles: '.jpg, .jpeg',
    autoProcessQueue: false, // myDropzone.processQueue() to upload dropped files
    addRemoveLinks: true,
    dictRemoveFile: "Remove image"
});

您可以在此处浏览他们的文档:

您可以使用以下代码使用被拒绝文件的计数来检测放置区域中是否存在错误

dropzone变量是dropzone对象


您可以使用以下代码使用被拒绝文件的计数来检测放置区域中是否存在错误

dropzone变量是dropzone对象

1步-来自服务器的响应 您必须根据错误/无错误的情况,从服务器向客户端发送不同的响应

// all good, no error (status code 200)
return response()->json(['message'=> $message, 'file' => $name], 200);

// Oh no, there is some error (look at the error code 400)
return response()->json(['message'=> $message, 'file' => $name], 400);
2步-从dropzone回调 这将在用户界面的正确位置显示上面定义的$message变量。

Dropzone.options.fileUploader = {
    addRemoveLinks: true, //your config
    dictRemoveFile: 'Remove file',

    init: function () {
        this.on("error", function (file, responseText) {
            var msgEl = $(file.previewElement).find('.dz-error-message');
            msgEl.text(responseText.message);
            msgEl.show();
        });
    }
}
1步-来自服务器的响应 您必须根据错误/无错误的情况,从服务器向客户端发送不同的响应

// all good, no error (status code 200)
return response()->json(['message'=> $message, 'file' => $name], 200);

// Oh no, there is some error (look at the error code 400)
return response()->json(['message'=> $message, 'file' => $name], 400);
2步-从dropzone回调 这将在用户界面的正确位置显示上面定义的$message变量。

Dropzone.options.fileUploader = {
    addRemoveLinks: true, //your config
    dictRemoveFile: 'Remove file',

    init: function () {
        this.on("error", function (file, responseText) {
            var msgEl = $(file.previewElement).find('.dz-error-message');
            msgEl.text(responseText.message);
            msgEl.show();
        });
    }
}

不太相关,但可能对将来的参考有用,您正在使用laravel framework发布,您的csrf令牌在哪里?你把它放在标题里了吗?这是一个隐藏的输入,但它被传递了。虽然不太相关,但可能对将来的参考有用,你是用laravel framework发布的,你的csrf令牌在哪里?你把它放在标题里了吗?这是一个隐藏的输入,但它被传递了。虽然不太相关,但可能对将来的参考有用,你是用laravel framework发布的,你的csrf令牌在哪里?你们把它放在标题里了吗?这是一个隐藏的输入,但它被忽略了。这不是一个特定于Dropzone的问题,而是一个特定于Laravel/Dropzone的组合问题。一旦Laravel验证失败,如果打开日志记录,您将看到以下内容:“文件”:[“文件字段是必需的。”]现在您有两个问题:1。如果没有CSS调整,Laravel将不会显示错误。2.简历将被删除,并且必须再次上传。我同意这篇文章所说的需要前端验证,不幸的是,没有提供代码示例。这不是特定于Dropzone的问题,而是特定于Laravel/Dropzone的组合问题。一旦Laravel验证失败,如果打开日志记录,您将看到以下内容:“文件”:[“文件字段是必需的。”]现在您有两个问题:1。如果没有CSS调整,Laravel将不会显示错误。2.简历将被删除,并且必须再次上传。我同意这篇文章所说的需要前端验证,不幸的是,没有提供代码示例。这不是特定于Dropzone的问题,而是特定于Laravel/Dropzone的组合问题。一旦Laravel验证失败,如果打开日志记录,您将看到以下内容:“文件”:[“文件字段是必需的。”]现在您有两个问题:1。如果没有CSS调整,Laravel将不会显示错误。2.简历将被删除,并且必须再次上传。我同意这篇文章所说的需要前端验证,不幸的是,没有提供代码示例。