validation - 验证失败时,Backbone Validate 正在发送请求

标签 validation backbone.js

根据有关验证的主干文档,它指出:

If validate returns an error, set and save will not continue, and the model attributes will not be modified.



因此,如果验证失败,我读取该设置或保存的方式不应该运行。但这不是我得到的结果。即使验证失败,它仍然会发送 POST/PUT 请求。我是在阅读文档错误还是在代码中做错了什么?

这是我的相关代码:
https://gist.github.com/80f6ef0099fbe96025dc
App.Models.Test = Backbone.Model.extend(
    urlRoot: '/api/test'

    validate: (attrs) ->
        errors = []
        if attrs.to is ''
            errors.push
                name: "to"
                field: "js-to"
                message: "You must enter a to address"
        if attrs.subject is ''
            errors.push
                name: "subject"
                field: "js-subject"
                message: "You must enter a subject"

        # Return our errors array if it isn't empty
        errors  if errors.length > 0
)

App.Views.Details = Backbone.View.extend(
    initialize: ->
        @model.bind "error", @error, this

    events:
        "click #js-save": "saveItem"

    saveItem: (e) ->
        e.preventDefault()

        # Set the model then save it.
        @model.set
            subject: $("#js-subject").val()
            message: $("#js-message").val()
            mailbox_id: $("#js-from").val()
            to: $("#js-to").val()
            cc: $("#js-cc").val()
            bcc: $("#js-bcc").val()
            tags: App.Helpers.tagsToObject $('#js-tags').val()
            scope: $('#js-scope').val()
            attachments: attachments

        @model.save null,
            success: (model, response) =>
                App.Helpers.showAlert "Success!", "Saved Successfully", "alert-success"
                @next()
            error: (model, response) ->
                App.Helpers.showAlert "Error", "An error occurred while trying to save this item", "alert-error"

    # Show the errors based on validation failure.
    error: (model, error) ->
        App.Helpers.displayValidationErrors error

最佳答案

你这样做是为了保存你的模型:

@model.save null,
    success: -> ...
    error:   -> ...

null是您麻烦的根源,请使用{}事情会开始变得更好;如果你结合你的@model.set@model.save打电话,事情会更好:
attrs =
    subject: $("#js-subject").val()
    #...
@model.save attrs,
    success: -> ...
    error:   -> ...

一个 save 调用看起来像这样:

save model.save([attributes], [options])
[...]
The attributes hash (as in set) should contain the attributes you'd like to change



所以通过 null对于 attributes表示您要按原样保存模型。

当你 save 一个模型,验证主要留给 set , code looks like this :
if (attrs && !this.set(attrs, options.wait ? silentOptions : options)) {
  return false;
}

您的 attrs将是 null所以set不会被调用;但是,如果您让 save处理您的set ,你会得到你所追求的行为。如果您通过了wait: true选项,savemanually run the validation关于传递的属性:
if (options.wait) {
  if (!this._validate(attrs, options)) return false;
  ...
}

内部 _validate 方法是 validate 的包装器做一些簿记和错误处理。你没有使用 wait: true所以这不适用于你,但我认为无论如何都值得一提。

考虑一个简单的示例,其模型 validate总是失败。如果你这样说:
@model.on 'error', @error
@model.save attrs,
    success: -> console.log 'AJAX success'
    error:   -> console.log 'AJAX error'

然后 @error将被调用,因为 save最终会调用setsome attributesset将调用 validate .演示:http://jsfiddle.net/ambiguous/HHQ2N/1/

但是,如果你说:
@model.save null, ...
null will cause the set call to be skipped .演示:http://jsfiddle.net/ambiguous/6pX2e/ (这里的 AJAX 会失败)。

您的 @model.set@model.save 之前调用应该触发您的错误处理程序,但如果您不检查 @model.set返回,执行将盲目地继续到 save调用并与您的服务器交谈。

总之,这里发生了三件事:
  • 你没有调用 save他们就是你应该的样子。
  • 您忽略了 @model.set返回值并失去捕获验证错误的机会。
  • Backbone 对 save(null, ...) 的参数处理可能会更好,但我不知道是否值得努力处理一种奇怪的调用方式。

  • 你应该结合你的set/save配对成一个 save或查看set返回。

    关于validation - 验证失败时,Backbone Validate 正在发送请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10983033/

    相关文章:

    c# - MVC 3 多个字段的远程验证

    jquery - 从无效的输入字段中获取 jQuery 值

    javascript - UnderscoreJS 未捕获类型错误 : Cannot call method 'replace' of undefined

    javascript - 定义主干模型类属性/字段的约定是什么

    javascript - mustache 格式的 Backbone /下划线模板导致#磅/哈希符号出错?

    java - 抛出消息包含违规值的异常是否正确或安全?

    javascript - 我有输入文本框,应采用 $ 和 £ 货币符号,并在用户键入任何其他特殊字符时发出警告

    php - 如何在 Laravel 自定义验证中获取其他字段值

    javascript - 在主干模型/ View 上处理更复杂的验证逻辑(必填字段等)的最佳方法?

    javascript - 避免 `fetch()` 将模型属性标记为已更改