骨干验证问题 [英] Backbone Validation issue

查看:130
本文介绍了骨干验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的骨干验证插件(可从以下 https://github.com/thedersen/backbone。验证)。
我还用引导3.我想验证的onChange或事件的onblur。

I used the Backbone validation plugin (available from: https://github.com/thedersen/backbone.validation). I also used Bootstrap 3. And I want to validate onChange or onBlur events.

我用它作为这样的:

在Models.js,我有这样的规则:

In Models.js, I have such rules:

   validation: {
       miliage: {
          required: true,
          pattern: 'number',
          msg: 'Укажите корректный пробег'
       },
       email : {
          required: true,
          pattern: 'email',
          msg: 'Укажите корректный email'
       },
    },

在Views.js呈现HTML后,我绑定验证插件:

In Views.js after rendering html I bind the validation plugin:

Backbone.Validation.bind( this, {
   valid: function( view, attr, selector ) {
       console.log('valid');
       console.log( attr );
       control = view.$('[' + selector + '=' + attr + ']')
       group = control.parents(".form-group")
       group.removeClass("has-error");
       group.find(".help-block").remove();
   },
   invalid: function( view, attr, error, selector ) {
       console.log('invalid');
       console.log( attr );
       control = view.$('[' + selector + '=' + attr + ']');
       console.log( control );
       group = control.parents(".form-group");
       group.addClass("has-error");
       group.find(".help-block").remove();
       group.append("<span for="+attr+" class='help-block'>"+error+"</span>");
  }

});

我显示使用Bootsrap规则的错误消息。

I display error messages using the Bootsrap rules.

在这个观点,我有这样的事件:

In this view, I have events like this:

 "change #email"     : "setEmail",
 "change #miliage"   : "setMiliage",

和相应的功能(我设置相应值模型):

And appropriate functions (I set values to the model accordingly):

setMiliage : function( event ) {       
     console.log( $( event.currentTarget ).val() );
     this.model.set('miliage', $(event.currentTarget).val(),{validate : true});
},
setEmail : function( event ) {
     this.model.set('email', $(event.currentTarget).val(),{validate :true})
},

正如你所看到的,我通过PARAMS验证:真实的方法集 - 为了每一次验证,当onChange事件触发

As you can see, I pass the params validate:true to method set - in order to validate each time when onChange event fires

问题:验证是在模型的各个方面(它的所有属性)是执行为什么回调函数的有效无效火是在验证规则中定义的每个属性。我有两个字段或更多需要验证,当其中的任何用户改变文本 - 在各个领域进行验证

Problem: Validation is performed on each aspect of the model (all of it's attributes) that is why the callback function valid and invalid fire on each attribute that is defined in the validation rules. I have two fields or more which require validation and when user change text in any of them - in all fields performed validation.

示例::当用户打开的第一次的页面 - 字段的miliage'和'邮件'是空的,当他们输入一个值到第一个字段(miliage)其他字段指示已发生的错误 - 因为它仍然是空的(用户还没有进入它尚未),我想显示消息仅在第一个字段。换言之,当然,第二字段是无效的,到目前为止,用户还没有得到那里。

Example: When the user opens the page for the first time - fields 'miliage' and 'email' are empty and when they typed a value into the first field (miliage) the other field indicates that an error has occurred - because it is still empty (user have not enter it yet) and i want to show message only for the first field. In other words, of course the second field is invalid so far, the user hasn't gotten there yet.

问:有没有在一个当前字段执行验证的唯一用户已经改变,显示错误不仅关系到它的方法吗?我也希望把值转换成模型时,它是无效的 - 我希望它只是显示错误。另外,我想在执行控制有效性提交表单(骨干Save方法之前)

Question: Is there a way to perform validation on one current field the only one the user has changed, and display errors only related to it? I also want to put value into a model when it is invalid - I want it to show errors only. Also, I want to perform control validation on submit form (before backbone save method)

我知道有哪些可以验证仅一个字段的isValid(场)的方法 - 但它并不触发有效或无效事件

I know there is the isValid(field) method which can validate only one field - but it does not trigger valid or invalid events

在此先感谢!

推荐答案

我解决了这个问题(仅校验改变输入)通过模型这种投入不能设置默认值 - 验证只适用于属性,已经在模型。当我需要的onChange的onblur验证我使用model.set('邮件','test@email.com',{验证:真}),当整个模型 - model.validate()或model.save()

I solved this problem (validate only changed inputs) by not setting default value in model for this inputs - validation works only on attributes, that are already in model. When i need onChange onBlur validation i use model.set('email','test@email.com',{validate:true}), when entire model - model.validate() or model.save()

这篇关于骨干验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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