简体   繁体   English

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

[英]Using ember-cp-validations across multi-page form

I have an order model that I'm collecting data for over a multi-page form. 我有一个order模型,可以通过多页表单收集数据。 I've created some validations like this: 我创建了一些这样的验证:

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),
});

My model is set up to use them like this: 我的模型设置为像这样使用它们:

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

What I'd like to happen is for it to only validate name and email when I'm on checkout.info and to validate address1 , address2 and city when I'm on checkout.shipping . 我想发生的是它仅验证nameemail ,当我在checkout.info并验证address1address2city ,当我在checkout.shipping

One of the things I've tried already is running the validations inside of my checkout-form component: 我已经尝试过的一件事是在checkout-form组件内部运行验证:

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

The problem is that this doesn't seem to unblock the disabled state on my form's next button 问题是,这似乎并没有取消阻止表单的下一个按钮上的禁用状态

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

Another thing I tried was to build a custom routed-presence validator that disables presence when it's not on the current route. 我想另一件事是建立一个自定义的routed-presence验证禁用presence时,它不是目前的路线上。 The trouble with this is that other validators will still block this (eg type or length). 麻烦的是其他验证器仍然会阻止此操作(例如类型或长度)。

How might I go about achieving this? 我将如何实现这一目标?

Although it's not well documented, you can enable or disable validations based on a condition that your model computes: 尽管没有充分记录,但您可以根据模型计算的条件启用或禁用验证:

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,
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM