簡體   English   中英

在多頁表單中使用ember-cp-validation

[英]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
})

我想發生的是它僅驗證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時,它不是目前的路線上。 麻煩的是其他驗證器仍然會阻止此操作(例如類型或長度)。

我將如何實現這一目標?

盡管沒有充分記錄,但您可以根據模型計算的條件啟用或禁用驗證:

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