Fixes: Force names of participants to be unique if anonymous participation is not allowed.

This commit is contained in:
jelhan 2016-06-21 01:13:09 +02:00
parent 1456828712
commit 1ab337d015
5 changed files with 36 additions and 17 deletions

View file

@ -12,7 +12,8 @@ const validCollection = function(collection) {
});
};
const Validations = buildValidations({
name: validator('presence', {
name: [
validator('presence', {
presence() {
// only force presence if anonymousUser poll setting is false
if (!this.get('model.anonymousUser')) {
@ -24,6 +25,16 @@ const Validations = buildValidations({
},
dependentKeys: ['anonymousUser', 'i18n.locale']
}),
validator('unique', {
parent: 'pollController.model',
attributeInParent: 'users',
dependentKeys: ['poll.users.[]', 'poll.users.@each.name', 'i18n.locale'],
disable() {
return this.get('model.anonymousUser');
},
messageKey: 'unique.name'
})
],
selections: [
validator('collection', true),

View file

@ -125,6 +125,7 @@ export default {
phone: '{{description}} muss eine gültige Telefonnummer sein',
url: '{{description}} muss eine gültige URL sein',
time: '{{value}} muss eine gültige Zeit sein',
unique: '{{description}} muss eindeutig sein'
unique: '{{description}} muss eindeutig sein',
'unique.name': 'Dieser Namen wurde bereits genutzt'
}
};

View file

@ -125,6 +125,7 @@ export default {
phone: '{{description}} must be a valid phone mumber',
url: '{{description}} must be a valid URL ',
time: '{{value}} must be a valid number',
unique: '{{description}} must be explicit'
unique: '{{description}} must be explicit',
'unique.name': 'This name has already been used'
}
};

View file

@ -125,6 +125,7 @@ export default {
phone: '{{description}} must be a valid phone number',
url: '{{description}} must be a valid url',
time: '{{value}} must be a valid time',
unique: '{{description}} must be unique'
unique: '{{description}} must be unique',
'unique.name': 'This name has already been used'
}
};

View file

@ -16,20 +16,25 @@ export default BaseValidator.extend({
Ember.isArray(options.dependentKeys) && options.dependentKeys.length > 0
);
if (options.disable) {
return true;
}
// ignore empty values
if (Ember.isEmpty(value)) {
return true;
}
const messageKey = options.messageKey || 'unique';
const parent = model.get(options.parent);
const collection = parent.get(options.attributeInParent);
const positionInCollection = collection.indexOf(model);
const elementsBefore = collection.slice(0, positionInCollection);
const elementsBefore = positionInCollection !== -1 ? collection.slice(0, positionInCollection) : collection;
const matches = elementsBefore.findBy(attribute, value);
if (matches) {
// ToDo: translateable error message
return this.createErrorMessage('unique');
return this.createErrorMessage(messageKey);
} else {
return true;
}