Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 验证主干表单的单个字段_Validation_Backbone.js_Coffeescript_Backbone Forms - Fatal编程技术网

Validation 验证主干表单的单个字段

Validation 验证主干表单的单个字段,validation,backbone.js,coffeescript,backbone-forms,Validation,Backbone.js,Coffeescript,Backbone Forms,我希望能够使用和一次验证表单中的单个字段,但是,如果我将需求放在模型验证中,而不是模式中,那么我在实现这一点时会遇到问题 我的模型是: class Event extends Backbone.Model url: -> '/events' + (if @isNew() then '' else '/' + @id) validation: title: required: true start_date: required: tr

我希望能够使用和一次验证表单中的单个字段,但是,如果我将需求放在模型验证中,而不是模式中,那么我在实现这一点时会遇到问题

我的模型是:

class Event extends Backbone.Model
  url: ->
    '/events' + (if @isNew() then '' else '/' + @id)

  validation:
    title:
      required: true
    start_date:
      required: true
    end_date:
      required: true

  schema: ->
    title:
      type: "Text"
    start_date:
      type: "DateTime"
      title: "Start Date"
      DateEditor: "DatePicker"
    end_date:
      type: "DateTime"
      title: "End Date"
      DateEditor: "DatePicker"
在我看来,使用这些的代码是

class Events extends Backbone.View
  ...
  initialize: ->
    @form = new Backbone.Form(model: @model).render()
    Backbone.Validation.bind @form;

  validateField: (field) ->
    @form.on "#{field}:change", (form, fieldEditor) =>
      @form.fields[field].validate()

  render: ->
    ...
    @validateField for field of @form.fields
    ...
但是,我的问题是,如果我像这样将
required:true
移动到模式中,它似乎才有效:

schema: ->
  title:
    type: "Text"
    validators: ["required"]
但是我不想这样做,因为backbone.validation有更广泛的内置验证器,我希望在本例之外使用这些验证器

我注意到主干形成状态

验证分为两个级别:模式验证器和常规内置主干模型验证。当调用form.commit()或form.validate()时,主干表单将同时运行

但是,我不确定在验证单个字段时它是否正在运行常规验证?这样做的原因是,当用户开始创建事件时,我不想验证他们尚未填写的字段

是否有任何方法可以在不必将验证移到模式中的情况下验证单个字段

提前谢谢

=======更新=======

查看单个字段的验证函数时,验证函数如下所示:

validate: function() {
  var $el = this.$el,
      error = null,
      value = this.getValue(),
      formValues = this.form ? this.form.getValue() : {},
      validators = this.validators,
      getValidator = Form.helpers.getValidator;

  if (validators) {
    //Run through validators until an error is found
    _.every(validators, function(validator) {
      error = getValidator(validator)(value, formValues);

      return error ? false : true;
    });
  }

  return error;
},
它似乎没有使用模型验证,所以我想知道如何将其合并