在多页表单中使用 ember-cp-validations

Using ember-cp-validations across multi-page form

我有一个 order 模型,我正在为多页表单收集数据。我创建了一些这样的验证:

const Validations = buildValidations({
  // these should only be used when we’re on checkout.info route
  name: validator('presence', true),
  email: [
    validator('presence', true),
    validator('format', { type: 'email' })
  ],
  // these should only be used when we’re on checkout.shipping route
  address1: validator('presence', true),
  address2: validator('presence', true),
  city: validator('presence', true),
});

我的模型设置为像这样使用它们:

export default Model.extend(Validations, {
  // model set-up in here
})

我希望它只在我处于 checkout.info 时验证 nameemail,并验证 address1address2city 当我在 checkout.shipping

我已经尝试过的其中一件事是 运行 我的 checkout-form 组件内部的验证:

let { m, validations } = order.validateSync({
  on: ['name', 'email']
})
const isValid = validations.get('isValid')
order.set('didValidate', isValid)

问题是这似乎无法取消阻止我表单的下一个按钮上的禁用状态

{{next-button disabled=(v-get model.order 'isInvalid')}}

我尝试的另一件事是构建一个自定义 routed-presence 验证器,当它不在当前路由上时禁用 presence。这样做的问题是其他验证器仍然会阻止它(例如类型或长度)。

我该如何实现这一目标?

虽然没有详细记录,但您可以根据模型计算的条件启用或禁用验证:

import { validator, buildValidations } from 'ember-cp-validations';

export default Ember.Object.extend(buildValidations({

  email: {
    disabled: Ember.computed.alias('model.isCheckoutPage'),
    validators: [
      ...
    ],
  }
}), {
  // could be a computed property too
  isCheckoutPage: false,
});