如何使用 ember-cp-validation 调用同一模型中的属性进行验证



>有没有办法从同一模型中调用属性?因为我想使用来自模型/代码.js的属性来计算同一文件中其他属性的验证器。我举个例子。

//model/code.js
import Ember from 'ember';
import DS from 'ember-data';
import {validator, buildValidations} from 'ember-cp-validations';
const CardValidations = buildValidations(
{
cardId: {
validators: [
validator('presence', true),
validator('length', {
// here instead of 10, I want to use nbBits
max: 10

}
]
}
}
);
export default Credential.extend(CardValidations, {
cardId: DS.attr('string'),
nbBits: DS.attr('number'),
displayIdentifier: Ember.computed.alias('cardId'),
});

正如你所看到的,我想调用nbBits,对cardId进行特定的验证。
有人知道方法或给我提示吗?谢谢你的时间

您的案例在ember-cp-validations的官方文档中描述如下:

const Validations = buildValidations({
username: validator('length', {
disabled: Ember.computed.not('model.meta.username.isEnabled'),
min: Ember.computed.readOnly('model.meta.username.minLength'),
max: Ember.computed.readOnly('model.meta.username.maxLength'),
description: Ember.computed(function() {
// CPs have access to the model and attribute
return this.get('model').generateDescription(this.get('attribute'));
}).volatile() // Disable caching and force recompute on every get call
})
});

您更简单的情况如下所示:

const CardValidations = buildValidations(
{
cardId: {
validators: [
validator('presence', true),
validator('length', {
// here instead of 10, I want to use nbBits
max: Ember.computed.readOnly('model.nbBits')
}
]
}
}
);