I try to use a custom-form in QGIS 3 to validate input-data before saving the data in a database. It would be nice to have the form open unless every input is correct. But the dialog/form closes even if the code detects wrong data...
...
my_dialog = None
inputField = None
def form_open(dialog, layerid, featureid):
global my_dialog
my_dialog=dialog
global inputField
inputField = my_dialog.findChild(QLineEdit,"qle_input")
buttonBox = my_dialog.findChild(QDialogButtonBox,"buttonBox")
buttonBox.accepted.disconnect()
buttonBox.accepted.connect(validate)
buttonBox.rejected.connect(my_dialog.resetValues)
def validate():
inputString=inputField.text() # get text from qleInput
pattern = re.compile("[^A-Za-z0-9]"); #pattern of not allowed data
if re.findall(pattern,inputString):
#if match (wrong characters) - give a warning and let the dialog open to change text in qle_input
textString='<b><center> Input contains wrong characters </center></b>'
msgBox = QMessageBox()
msgBox.setText(textString)
msgBox.exec_()
else: # Close dialog
my_dialog.close