decide.nolog.cz/app/validators/unique.js

38 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-05-20 21:49:29 +02:00
import BaseValidator from 'ember-cp-validations/validators/base';
import Ember from 'ember';
export default BaseValidator.extend({
validate(value, options, model, attribute) {
Ember.assert(
'options.parent is required',
Ember.isPresent(options.parent)
);
Ember.assert(
'options.attributeInParent is required',
Ember.isPresent(options.attributeInParent)
);
Ember.assert(
'options.dependentKeys is required',
Ember.isArray(options.dependentKeys) && options.dependentKeys.length > 0
);
// ignore empty values
if (Ember.isEmpty(value)) {
return true;
}
const parent = model.get(options.parent);
const collection = parent.get(options.attributeInParent);
const positionInCollection = collection.indexOf(model);
const elementsBefore = collection.slice(0, positionInCollection);
const matches = elementsBefore.findBy(attribute, value);
if (matches) {
// ToDo: translateable error message
return this.createErrorMessage('unique');
} else {
return true;
}
}
});