2017-04-25 65 views
1

我在用於驗證的ember js應用程序中使用了ember-cp-validation。在組件頁面中使用validate()方法。但我收到錯誤(驗證不是一個函數)。我提到這個linkvalidate()不是ember-cp-validation中的函數

在模型頁面(profile.js),

import DS from 'ember-data'; 
import { validator, buildValidations } from 'ember-cp-validations'; 

const Validations = buildValidations({ 
    name: validator('presence', true), 

    address:[ 
     validator('presence', true), 
     validator('length', { max: 300}), 
    ], 

    pincode: validator('presence', true), 

    email:[ 
     validator('presence', true), 
     validator('format', {type:'email'}) 
    ] 

}); 

export default DS.Model.extend(Validations,{ 
    name: DS.attr('string'), 
    address: DS.attr('string'), 
    pincode: DS.attr('number'), 
    email: DS.attr('string') 
}); 

和組件頁面,

import Ember from 'ember'; 

export default Ember.Component.extend({  
    actions: { 
     authenticate() {    
      let profile = this.get('profile'); 
      profile.validate().then(({ validations }) => { 
       if(validations.get('isValid')){ 
        this.transitionToRoute("welcome"); 
       } 
      });   
     } 
    } 
}); 
+0

你確定'組件的profile'屬性確實已爲其定義驗證了'model'的實例? – alptugd

+0

'profile'是模型文件名。 – Arunprasath

回答

0

在我的組件我使用:

  • this.get('model').validate()強制驗證,
  • this.get('isValid')知道當前輸入是否有效。

所以,你可以這樣做:

this.get('model').validate().then(() => { 
    if (this.get('isValid')) { 
    this.transitionToRoute("welcome"); 
    } 
}); 
相關問題