javascript - 区分图像错误和形式错误

标签 javascript jquery laravel laravel-4 dropzone.js

我正在使用dropzone.js 。我有一个表单,单击提交按钮时会提交输入和图像。但是,当我返回输入之一(不是图像)错误的错误时,error事件还将显示图像有错误,即使事实并非如此。

例如,如果其中一个输入为空,并且表单返回错误并显示消息“请填写表单”,则上传的图像也会(错误地)出现错误,并且 HTML 中的情况会发生变化:

<div class="dz-error-message"><span data-dz-errormessage="">[object Object]</span></div>

实际返回的错误(注意图像没有错误):

{"success":"false","messages":{"message":["The message field is required."]}}

我的问题是,如何修改代码来识别返回的错误是图像错误还是表单输入错误?

这是我的 JS:

Dropzone.options.formPost = {
    autoProcessQueue: false,
    uploadMultiple: true,
    parallelUploads: 25,
    maxFiles: 25,
    maxFilesize: 5,
    acceptedFiles: "image/*",
    clickable: "#image-upload",

    init: function() {
        var myDropzone = this;
        var form = $("#form-post");

        this.element.querySelector('button[type="submit"]').addEventListener("click", function(e)
        {
            e.preventDefault();
            e.stopPropagation();
            myDropzone.processQueue();
        });

        this.on("addedfile", function()
        {
            // some stuff
        });
        this.on("sendingmultiple", function()
        {
            // some stuff
        });
        this.on("successmultiple", function(files, response)
        {
            // some stuff
        });
        this.on("errormultiple", function(files, response)
        {
            // if an error is return, even though it has nothing to do with the image, it will show that the image has an error
            $.each(response.messages, function(key, value)
            {
                form.find('.form-group#' + key).addClass('has-error');
                form.find('.form-group#' + key + ' .form-error').html(value[0]).fadeIn();
            });
        });
    }
}

PHP(Laravel):

public function createPost()
{
    $rules = array(
        'message' => 'required',
    );

    $validator = Validator::make(Input::except('file'), $rules);

    if ($validator->fails())
    {
        return Response::json(array(
            'success' => 'false',
            'messages' => $validator->messages()
        ), 400);
    }
    else
    {
        $post = new Post;
        $post->user_id = getUserID();
        $post->message = Input::get('message');
        $post->ip_address = getUserIP();
        $post->save();

        $files = Input::file('file');

        if ($files)
        {
            /**
             * Create a new directory for the post images.
             */
            File::makeDirectory(public_path() . '/uploads/posts/' . $post->id, 0775);

            foreach ($files as $file)
            {
                $file_rules = array('file' => 'image|max:5000');
                $validator = Validator::make(array('file' => $file), $file_rules);

                if ($validator->fails())
                {
                    return Response::json(array(
                        'success' => 'false',
                        'messages' => $validator->messages()
                    ), 400);
                }
                else
                {
                    $ext = $file->getClientOriginalExtension();
                    $dest = public_path() . '/uploads/posts/' . $post->id . '/';
                    $file_name = str_random(6);

                    $upload_success = $file->move($dest, $file_name . '.' . $ext);

                    if (!$upload_success)
                    {
                        return Response::json(array(
                            'success' => 'false',
                        ), 400);
                    }
                }
            }
        }

        return Response::json(array(
            'success' => 'true'
        ), 200);
    }
}

最佳答案

下面的代码可能会更改图像字段,而实际上它不应该这样做。

form.find('.form-group#' + key).addClass('has-error');
form.find('.form-group#' + key + ' .form-error').html(value[0]).fadeIn();

您可以在浏览器的 JS 控制台中手动检查 form.find('.form-group#whatever') 是否确实能够指向正确的元素。也许 form.find('.form-group#whatever') 指向所有输入元素,这会导致问题。

查看 HTML 代码也会很有帮助。

关于javascript - 区分图像错误和形式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991148/

相关文章:

javascript - 选择器 Jquery 中的选择器

javascript - 无法在 <Formik> 字段中键入

jquery - 使用 jQuery 的悬停状态

php - Laravel 4.1 - 使用 Phpunit 和 Mockery 测试 Artisan 命令

javascript - JavaScript 不可预测的行为

javascript - window.open() 在 Knockout.js 中不起作用

javascript - 如何使用 JavaScript 从 "data"属性读取数据

JavaScript 宾果游戏 - 使单元格可点击

javascript - for 循环中添加数字不断增加的新输入时出现的问题

javascript - 数据库驱动的内容和语言环境