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");
}
});
}
}
});
你確定'組件的profile'屬性確實已爲其定義驗證了'model'的實例? – alptugd
'profile'是模型文件名。 – Arunprasath