1
0

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.
This commit is contained in:
f321x
2026-01-26 14:29:58 +01:00
parent c3d1b2046a
commit 5952d8c614
4 changed files with 32 additions and 1 deletions

View File

@@ -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

View File

@@ -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
}
}
}

View File

@@ -5,7 +5,7 @@ import QtQuick.Controls
import "../controls"
WizardComponent {
valid: true
valid: sc.addressValid
last: true
title: qsTr('Server')

View File

@@ -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()