我有一个订单
模型,我正在通过多页表单收集数据。我创建了一些这样的验证:
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
时验证name
和 email
并当我使用 checkout.shipping
时,验证 address1
、address2
和 city
。
我已经尝试过的事情之一是在我的 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,
});
关于javascript - 跨多页面表单使用 ember-cp-validations,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45343328/