[英]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
})
我想發生的是它僅驗證name
和email
,當我在checkout.info
並驗證address1
, address2
和city
,當我在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,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.