[pve-devel] [PATCH manager] use validator for GuestIDSelector and optimize labels

Dominik Csapak d.csapak at proxmox.com
Tue Feb 21 10:53:15 CET 2017


instead of using markInvalid, use a validator, which also marks the
field dirty and marks the form correctly invalid.

also optimize the label/error message assignments

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 www/manager6/form/GuestIDSelector.js | 50 ++++++++++++++++++++++++------------
 1 file changed, 33 insertions(+), 17 deletions(-)

diff --git a/www/manager6/form/GuestIDSelector.js b/www/manager6/form/GuestIDSelector.js
index 298988d..0d4b85c 100644
--- a/www/manager6/form/GuestIDSelector.js
+++ b/www/manager6/form/GuestIDSelector.js
@@ -14,28 +14,46 @@ Ext.define('PVE.form.GuestIDSelector', {
 
     guestType: undefined,
 
+    validator: function(value) {
+	var me = this;
+
+	if (!Ext.isNumeric(value) ||
+	    value < me.minValue ||
+	    value > me.maxValue) {
+	    // check is done by ExtJS
+	    return;
+	}
+
+	if (me.validateExists === true && !me.exists) {
+	    return me.unknownID;
+	}
+
+	if (me.validateExists === false && me.exists) {
+	    return me.inUseID;
+	}
+
+	return true;
+    },
+
     initComponent: function() {
 	var me = this;
 	var label = '{0} ID';
 	var unknownID = gettext('This {0} ID does not exists');
 	var inUseID = gettext('This {0} ID is already in use');
+	var type = 'CT/VM';
 
 	if (me.guestType === 'lxc') {
-	    label = Ext.String.format(label, 'CT');
-	    unknownID = Ext.String.format(unknownID, 'CT');
-	    inUseID = Ext.String.format(inUseID, 'CT');
+	    type = 'CT';
 	} else if (me.guestType === 'qemu') {
-	    label = Ext.String.format(label, 'VM');
-	    unknownID = Ext.String.format(unknownID, 'VM');
-	    inUseID = Ext.String.format(inUseID, 'VM');
-	} else {
-	    label = Ext.String.format(label, 'CT/VM');
-	    unknownID = Ext.String.format(unknownID, 'CT/VM');
-	    inUseID = Ext.String.format(inUseID, 'CT/VM');
+	    type = 'VM';
 	}
 
+	me.label = Ext.String.format(label, type);
+	me.unknownID = Ext.String.format(unknownID, type);
+	me.inUseID = Ext.String.format(inUseID, type);
+
 	Ext.apply(me, {
-	    fieldLabel: label,
+	    fieldLabel: me.label,
 	    listeners: {
 		'change': function(field, newValue, oldValue) {
 		    if (!Ext.isDefined(me.validateExists)) {
@@ -46,14 +64,12 @@ Ext.define('PVE.form.GuestIDSelector', {
 			url: '/cluster/nextid',
 			method: 'GET',
 			success: function(response, opts) {
-			    if (me.validateExists === true) {
-				me.markInvalid(unknownID);
-			    }
+			    me.exists = false;
+			    me.isValid();
 			},
 			failure: function(response, opts) {
-			    if (me.validateExists === false) {
-				me.markInvalid(inUseID);
-			    }
+			    me.exists = true;
+			    me.isValid();
 			}
 		    });
 		}
-- 
2.1.4





More information about the pve-devel mailing list