【问题标题】:validate() is not a function in ember-cp-validationvalidate() 不是 ember-cp-validation 中的函数
【发布时间】:2017-04-25 12:24:39
【问题描述】:

我在 ember js 应用程序中使用 ember-cp-validation 进行验证。在组件页面中使用 validate() 方法。但我收到错误(验证不是函数)。我提到了这个link

在模型页面(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 的一个实例吗?
  • 'profile' 是模型文件名。

标签: ember.js ember-cli ember-cp-validations


【解决方案1】:

在我正在使用的组件中:

  • this.get('model').validate() 强制验证,
  • this.get('isValid') 知道当前输入是否有效。

所以你可以这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    相关资源
    最近更新 更多