2017-07-27 49 views
0

我有一個order模型,我正在通過多頁表單收集數據。我已經創造了一些驗證這樣的:在多頁面表單中使用ember-cp驗證

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

我想發生的是它僅驗證nameemail當我在checkout.info上時,驗證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時,它不是目前的路線上。這樣做的麻煩在於其他驗證器仍然會阻止這個(例如類型或長度)。

我該如何去做到這一點?

回答

1

雖然它沒有很好的記載,可以啓用或模型計算禁用基於條件的驗證:

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

很好,謝謝!我會如何將當前的路線信息傳遞到我的模型中?例如能夠檢查像'computed.equal('model.currentPath','checkout.shipping')'? – gosseti

+0

我已經添加了'step:attr('string')'到我的'訂單'模型。在我的'checkout-form'組件中,我做了'this.get('model.order')。setProperties({step:this.get('router.currentPath')})',然後'disabled:!computed .equal('model.step','checkout.shipping')'在我的驗證中。它似乎沒有工作,但...我做了什麼顯然是錯誤的? – gosseti

+1

相信你需要使用第二個計算屬性'disabled:computed.not()'或者一個自定義的計算屬性,我不認爲你可以像你在做的那樣否定... – acorncom

相關問題