1

有没有办法从同一个模型中调用一个属性?因为我想使用 model/code.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进行特定验证。
有人知道方法或给我提示吗?感谢您的时间

4

1 回答 1

1

您的案例在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')
                }
            ]
        }
    }
);
于 2017-08-09T08:14:31.553 回答