【问题标题】:Dropzone.js and form validationDropzone.js 和表单验证
【发布时间】:2016-04-21 14:59:18
【问题描述】:

是否可以不破坏在将整个表单标记为“dropzone”类后显示的漂亮 Rails 表单验证错误?

现在,当我尝试提交表单时,没有任何变化,页面保持不变,没有为用户提供任何信息,哪些字段不符合要求。控制器呈现似乎没有被视图处理的 JSON 响应(使用结果的 dropzone)。

提前感谢您的快速回复。

【问题讨论】:

  • 您需要自己将 JSON 响应解析为您的 js 回调。如果您需要帮助,请告诉我们。
  • 好的,我应该在哪个 dropzone 列表器中解析 JSON 响应以及如何将其传递给 Rails 表单以正确显示输入错误?
  • 看看这个事件:dropzonejs.com/#event-success
  • 请看我的回答。告诉我进展如何。

标签: ruby-on-rails json forms validation dropzone.js


【解决方案1】:

我对这个问题的解决方法:

Dropzone.options.filedrop = {
    init: function () {
        // Other triggers not listed
        // ...

        this.on("error", function (file, response) {
            // Gets triggered when the files have successfully been sent.
            // Redirect user or notify of success.

            // Build an unordered list from the list of errors
            var _ref, _results, _i, _len;
            if ($.isPlainObject(response)) { // Check if response is in JSON format for showing standard form errors
                // Remove errors from accepted image
                file.previewElement.classList.remove("dz-error");
                _ref = file.previewElement.querySelectorAll("[data-dz-errormessage]");
                _results = [];
                for (_i = 0, _len = _ref.length; _i < _len; _i++) {
                    node = _ref[_i];
                    _results.push(node.textContent = null);
                }

                file['status'] = 'success';
                console.log("acceptedImage: " + JSON.stringify(file));

                var json = response;
                var errorText = "<div id=\"error_explanation\"><h2>" + Object.keys(json).length + " errors prohibited this thing from being saved:</h2><ul>";
                $.each(json, function (key, value) {
                    errorText += "<li>" + key.capitalize() + ' ' + value[0] + "</li> ";
                });
                errorText += "</ul></div>";
                console.log("errorText: " + errorText);

                // Insert error list into form
                $('.errors_placeholder').html(errorText);
            } else {
                if (myDropzone.files.length > 1) {
                    myDropzone.removeFile(myDropzone.files[0]);
                }
            }
        });
    }
};

【讨论】:

    【解决方案2】:

    您需要自己将 JSON 响应解析为您的 js 回调,如下所示:

       Dropzone.options.filedrop = {
          init: function () {
            this.on("complete", function (file, jsonResponse) {
              console.log(jsonResponse);
              //do stuff with the jsonResponse here
            });
          }
        };
    

    【讨论】:

    • 谢谢,我自己想通了,但改用 error 事件。有什么办法可以将错误直接传递给表单,并省略在 JS 中从头开始渲染错误部分?
    • @Kaktusiarz 你能更新你的代码来展示你是如何处理这个问题的吗?
    • @Kaktusiarz 你能帮忙解决这个问题吗?stackoverflow.com/questions/39056366/…
    猜你喜欢
    • 1970-01-01
    • 2018-07-30
    • 2012-07-29
    • 1970-01-01
    • 2016-06-28
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多