使用Backbone.Validation跳过模型属性的验证 [英] Skip validation of model attribute using Backbone.Validation

查看:210
本文介绍了使用Backbone.Validation跳过模型属性的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动态渲染的视图.它可能有一些输入,也可能没有. 用户填满所有内容并尝试发送数据后,我呼叫this.model.isValid(true)(或this.model.isValid()),即使来自输入的数据有效,它也会返回false.

I have a view that is rendered dynamically. It may have some inputs or may not have. After a user fills everything and tries to send data I call this.model.isValid(true) ( or this.model.isValid() ) and it returns false even if the data from inputs is valid.

我认为原因是骨干验证试图验证我们未呈现的输入的属性.

I think the cause is Backbone Validation tries to validate attributes of inputs we did not render.

如果没有坚持视图元素?

更新:

我的 模型 与此类似:

My model is similar to this:

MyApp.module("RecordModel", function (RecordModel, MyApp, Backbone) {
    RecordModel.recordModel = Backbone.Model.extend({
        validation: {
            inn: {
                pattern: 'inn',
                msg: MyApp.messages.inn
            },

            bik: {
                pattern: 'bik',
                msg: MyApp.messages.bik
            },

            uin: {
                pattern: 'uin',
                msg: MyApp.messages.uin
            },

            sum: {
                pattern: 'sum',
                msg: MyApp.messages.sum
            }

        }
    });
});

绑定:

Bindings:

bindings: {

    '#uin': {
        observe: 'uin',
            setOptions: {
            validate: true
        },

        events: MyApp.Validation.events.inputEvents
    },

    '#bik': {
        observe: 'bik',
            setOptions: {
            validate: true
        },
        events: MyApp.Validation.events.inputEvents
    },

    '#inn': {
        observe: 'inn',
            setOptions: {
            validate: true
        },
        events: ParkingMate.Validation.events.inputEvents
    },

    '#sum': {
        observe: 'sum',
            setOptions: {
            validate: true
        },
        events: MyApp.Validation.events.inputEvents
    }
}

因此由于某些原因,我们不会渲染例如#sum的输入.由于我们的DOM中没有它,因此它在RecordModel中不存在,但是主干仍然尝试对其进行验证.或者,如果我们在DOM中有此输入,则一切正常.

So for some reason we din't render #sum input for instance. As we haven't it got in our DOM, it doesn't exists in RecordModel, but backbone still tries to validate it. Or if we have this input in our DOM, everything works fine.

推荐答案

如何允许空值,但仍然可以验证用户是否输入了某些内容?

默认情况下,如果为属性配置验证器,则为 被认为是必需的.但是,如果您想允许使用空值和 在输入内容时仍然可以验证,请在其中添加required: false 除了其他验证器.

By default, if you configure a validator for an attribute, it is considered required. However, if you want to allow empty values and still validate when something is entered, add required: false in addition to other validators.

validation: {
    value: {
        min: 1,
        required: false
    }
}

如果您不能让空值(如创建时),则 Backbone.validation会覆盖isValid 将功能添加到默认行为.有趣的是我们可以传递的数组参数:

If you can't let empty values (like at creation), Backbone.validation overrides isValid adding features to the default behavior. What's interesting is the array parameter we can pass:

// Check if name and age are valid
var isValid = model.isValid(['name', 'age']);

这样,我们就可以仅验证当前模型中存在的字段:

With this, we can then validate only the fields that exist within the model at the moment:

model.isValid(_.keys(model.attributes));

这篇关于使用Backbone.Validation跳过模型属性的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆