From 5952d8c6147d51e5c22234ae2fddbe7f5a75feb3 Mon Sep 17 00:00:00 2001 From: f321x Date: Mon, 26 Jan 2026 14:29:58 +0100 Subject: [PATCH] qml: validate server address in ServerConfig Same as Qt, validate the server address the user enters and prevent the user from continuing in the wizard or clicking 'Ok' in the ServerConfig if the entered address is clearly invalid. --- .../gui/qml/components/ServerConfigDialog.qml | 1 + .../qml/components/controls/ServerConfig.qml | 26 +++++++++++++++++++ .../qml/components/wizard/WCServerConfig.qml | 2 +- electrum/gui/qml/qenetwork.py | 4 +++ 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/electrum/gui/qml/components/ServerConfigDialog.qml b/electrum/gui/qml/components/ServerConfigDialog.qml index 80cc54a31..9fed62953 100644 --- a/electrum/gui/qml/components/ServerConfigDialog.qml +++ b/electrum/gui/qml/components/ServerConfigDialog.qml @@ -39,6 +39,7 @@ ElDialog { FlatButton { Layout.fillWidth: true text: qsTr('Ok') + enabled: serverconfig.addressValid icon.source: '../../icons/confirmed.png' onClicked: { let auto_connect = serverconfig.serverConnectMode == ServerConnectModeComboBox.Mode.Autoconnect diff --git a/electrum/gui/qml/components/controls/ServerConfig.qml b/electrum/gui/qml/components/controls/ServerConfig.qml index 47bd4e680..aae493bae 100644 --- a/electrum/gui/qml/components/controls/ServerConfig.qml +++ b/electrum/gui/qml/components/controls/ServerConfig.qml @@ -12,6 +12,7 @@ Item { property bool showAutoselectServer: true property alias address: address_tf.text property alias serverConnectMode: server_connect_mode_cb.currentValue + property alias addressValid: address_tf.valid implicitHeight: rootLayout.height @@ -28,6 +29,11 @@ Item { ServerConnectModeComboBox { id: server_connect_mode_cb + onCurrentValueChanged: { + if (currentValue == ServerConnectModeComboBox.Mode.Autoconnect) { + address_tf.text = "" + } + } } Item { @@ -63,6 +69,26 @@ Item { enabled: server_connect_mode_cb.currentValue != ServerConnectModeComboBox.Mode.Autoconnect width: parent.width inputMethodHints: Qt.ImhNoPredictiveText + + property bool valid: true + + function validate() { + if (!enabled) { + valid = true + return + } + valid = Network.isValidServerAddress(address_tf.text) + } + + onTextChanged: validate() + onEnabledChanged: validate() + + Rectangle { + anchors.fill: parent + color: "red" + opacity: 0.2 + visible: !parent.valid + } } } diff --git a/electrum/gui/qml/components/wizard/WCServerConfig.qml b/electrum/gui/qml/components/wizard/WCServerConfig.qml index bc784d1b6..b0ab17082 100644 --- a/electrum/gui/qml/components/wizard/WCServerConfig.qml +++ b/electrum/gui/qml/components/wizard/WCServerConfig.qml @@ -5,7 +5,7 @@ import QtQuick.Controls import "../controls" WizardComponent { - valid: true + valid: sc.addressValid last: true title: qsTr('Server') diff --git a/electrum/gui/qml/qenetwork.py b/electrum/gui/qml/qenetwork.py index b25712d13..fa088822d 100644 --- a/electrum/gui/qml/qenetwork.py +++ b/electrum/gui/qml/qenetwork.py @@ -206,6 +206,10 @@ class QENetwork(QObject, QtEventListener): def server(self): return self._server + @pyqtSlot(str, result=bool) + def isValidServerAddress(self, server: str) -> bool: + return ServerAddr.from_str_with_inference(server) is not None + @pyqtSlot(str, bool, bool) def setServerParameters(self, server_str: str, auto_connect: bool, one_server: bool): net_params = self.network.get_parameters()