From ef027160d59f03573a0a5dbbb5293c3c40b3f25e Mon Sep 17 00:00:00 2001
From: trendschau <trendschau@gmail.com>
Date: Sat, 31 Oct 2020 14:53:37 +0100
Subject: [PATCH] Version 1.4.1: Custom fields finished

---
 plugins/customfields/customfields.yaml  |  2 +-
 plugins/customfields/js/customfields.js |  2 +-
 system/Models/Validation.php            |  4 +--
 system/author/js/vue-meta.js            | 46 +++++++++++++++++--------
 4 files changed, 36 insertions(+), 18 deletions(-)

diff --git a/plugins/customfields/customfields.yaml b/plugins/customfields/customfields.yaml
index 8392df2..7cafbcd 100644
--- a/plugins/customfields/customfields.yaml
+++ b/plugins/customfields/customfields.yaml
@@ -6,7 +6,7 @@ homepage: http://trendschau.net
 licence: No licence
 
 metatabs:
-  adamhall:
+  customfields:
     fields:
       myfield:
         type: customfields
diff --git a/plugins/customfields/js/customfields.js b/plugins/customfields/js/customfields.js
index 357e61e..be5a6f6 100644
--- a/plugins/customfields/js/customfields.js
+++ b/plugins/customfields/js/customfields.js
@@ -1,4 +1,4 @@
-Vue.component('tab-adamhall', {
+Vue.component('tab-customfields', {
 	props: ['saved', 'errors', 'formdata', 'schema'],
 	template: '<section><form>' +
 				'<component v-for="(field, index) in schema.fields"' +
diff --git a/system/Models/Validation.php b/system/Models/Validation.php
index 972c347..5adc067 100644
--- a/system/Models/Validation.php
+++ b/system/Models/Validation.php
@@ -61,7 +61,7 @@ class Validation
 		        }
 			}
 			return true;
-		}, 'contains one or more invalid ip-adress');
+		}, 'contains one or more invalid ip-adress.');
 
 		Validator::addRule('customfields', function($field, $value, array $params, array $fields) use ($user)
 		{
@@ -78,7 +78,7 @@ class Validation
 				}
 			}
 			return true;
-		}, 'contains one or more invalid values');
+		}, 'some fields are empty or contain invalid values.');
 
 		Validator::addRule('checkPassword', function($field, $value, array $params, array $fields) use ($user)
 		{
diff --git a/system/author/js/vue-meta.js b/system/author/js/vue-meta.js
index b909d8f..9203a7f 100644
--- a/system/author/js/vue-meta.js
+++ b/system/author/js/vue-meta.js
@@ -382,11 +382,11 @@ Vue.component('component-customfields', {
 			fielddetails: {},
 		 }
 	},
-
 	template: '<div class="large">' +
 				'<label class="mb2">{{ label|translate }}</label>' +
-			  	'<div v-if="fielderrors" class="error mb2"><small>{{ fielderrors }}</small></div>' +
-			  	'<div v-else="description" class="fielddescription mb2"><small>{{ description|translate }}</small></div>' +
+			  	'<div class="fielddescription mb2 f7">{{ description|translate }}</div>' +
+			  	'<div v-if="errors[name]" class="error mb2 f7">{{ errors[name] }}</div>' +
+			  	'<div v-if="fielderrors" class="error mb2 f7">{{ fielderrors }}</div>' +
 	  			'<div class="customrow flex items-start mb3" v-for="(pairobject, pairindex) in value">' +
 					'<input type="text" placeholder="key" class="customkey" :class="pairobject.keyerror" :value="pairobject.key" @input="updatePairKey(pairindex,$event)">' +
 			  		'<div class="mt3"><svg class="icon icon-dots-two-vertical"><use xlink:href="#icon-dots-two-vertical"></use></svg></div>' + 
@@ -405,24 +405,42 @@ Vue.component('component-customfields', {
 	methods: {
 		update: function(value, name)
 		{
+			this.fielderrors = false;
+			this.errors = false;
 			FormBus.$emit('forminput', {'name': name, 'value': value});
 		},
 		updatePairKey: function(index,event)
 		{
 			this.value[index].key = event.target.value;
-			
+
 			var regex = /^[a-z0-9]+$/i;
-			if(regex.test(event.target.value))
-			{
-				this.fielderrors = false;
-				delete this.value[index].keyerror;
-				this.update(this.value,this.name);
-			}
-			else
+
+			if(!this.keyIsUnique(event.target.value,index))
 			{
 				this.value[index].keyerror = 'red';
-				this.fielderrors = 'Only alphanumeric for keys allowed';
+				this.fielderrors = 'Error: The key already exists';
+				return;
 			}
+			else if(!regex.test(event.target.value))
+			{
+				this.value[index].keyerror = 'red';
+				this.fielderrors = 'Error: Only alphanumeric for keys allowed';
+				return;
+			}
+
+			delete this.value[index].keyerror;
+			this.update(this.value,this.name);
+		},
+		keyIsUnique: function(keystring, index)
+		{
+			for(obj in this.value)
+			{
+				if( (obj != index) && (this.value[obj].key == keystring) )
+				{
+					return false;
+				}
+			}
+			return true;
 		},
 		updatePairValue: function(index, event)
 		{
@@ -432,11 +450,10 @@ Vue.component('component-customfields', {
 			if(event.target.value == '' || regex.test(event.target.value))
 			{
 				this.value[index].valueerror = 'red';
-				this.fielderrors = 'No empty values or html tags are allowed';				
+				this.fielderrors = 'Error: No empty values or html tags are allowed';				
 			}
 			else
 			{
-				this.fielderrors = false;
 				delete this.value[index].valueerror;
 				this.update(this.value,this.name);
 			}
@@ -456,6 +473,7 @@ Vue.component('component-customfields', {
 		deleteField: function(index)
 		{
 			this.value.splice(index,1);
+			this.update(this.value,this.name);
 		},
 	},
 })