区分图像错误和形成错误

时间:2015-06-22 22:50:47

标签: 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);
    }
}

1 个答案:

答案 0 :(得分:0)

以下代码可能会对图像字段进行更改,但不应该这样做。

form.find('.form-group#whatever')

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

查看HTML代码也很有帮助。