duanchi0883649 2015-06-12 07:02
浏览 82

Dropzone Laravel表单验证错误?

Problem: Form post validation on images fails. Error: Images are required

In my validation rules, i've set:

'images' => 'required|image',

In my form i've built the following:

<div class="panel panel-default">

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

    <div class="panel-body">

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

        </div>

    </div>

</div>

I'm not sure how to insert the file upload form field? I am expecting the validator to throw the error. But with regards to dropzone, i'm not sure how to do it.

This is my javascript to initiate dropzone

Dropzone.autoDiscover = false;

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

My Ajax request to post the form is this:

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

    }

});

Thank you for reading

  • 写回答

1条回答 默认 最新

  • dougou7782 2016-10-31 19:46
    关注

    Dropzone sets the file field to 'file' on default.

    You can change it by modifying your .dropzone() function to this:

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

    You can browse their documementation here: http://www.dropzonejs.com/#configuration

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试