Bläddra i källkod

Adjustments to input validation

Moved RegExes for validators into OPL namespace, removed some QRegularExpressionValidators (give the user more freedom what they want to put in the database)
Felix Turowsky 1 år sedan
förälder
incheckning
7b6ad04aa2

+ 2 - 2
src/gui/dialogues/newflightdialog.cpp

@@ -105,12 +105,12 @@ void NewFlightDialog::setupRawInputValidation()
 {
     // Time Line Edits
     for (const auto& line_edit : *timeLineEdits) {
-        auto validator = new QRegularExpressionValidator(QRegularExpression("([01]?[0-9]|2[0-3]):?[0-5][0-9]?"), line_edit);
+        const auto validator = new QRegularExpressionValidator(OPL::RegEx::RX_TIME_ENTRY, line_edit);
         line_edit->setValidator(validator);
     }
     // Location Line Edits
     for (const auto& line_edit : *locationLineEdits) {
-        auto validator = new QRegularExpressionValidator(QRegularExpression("[a-zA-Z0-9]{1,4}"), line_edit);
+        const auto validator = new QRegularExpressionValidator(OPL::RegEx::RX_AIRPORT_CODE, line_edit);
         line_edit->setValidator(validator);
         line_edit->setCompleter(QCompleterProvider.getCompleter(CompleterProvider::Airports));
     }

+ 2 - 16
src/gui/widgets/settingswidget.cpp

@@ -175,22 +175,8 @@ void SettingsWidget::readSettings()
 
 void SettingsWidget::setupValidators()
 {
-    const QHash<QLineEdit*, QRegularExpression> validator_map = {
-        {ui->firstnameLineEdit, QRegularExpression(QLatin1String("\\w+"))},
-        {ui->lastnameLineEdit, QRegularExpression(QLatin1String("\\w+"))},
-        {ui->phoneLineEdit, QRegularExpression(QLatin1String("^[+]{0,1}[0-9\\-\\s]+"))},
-        {ui->emailLineEdit, QRegularExpression(QString("\\A[a-z0-9!#$%&'*+/=?^_‘{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_‘{|}~-]+)*@"
-         "(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\z"))},
-        {ui->companyLineEdit, QRegularExpression(QLatin1String("\\w+"))},
-        {ui->employeeidLineEdit, QRegularExpression(QLatin1String("\\w+"))},
-        {ui->prefixLineEdit, QRegularExpression(QLatin1String("\\w+"))},
-    };
-
-    QHash<QLineEdit*, QRegularExpression>::const_iterator i;
-    for (i = validator_map.constBegin(); i != validator_map.constEnd(); ++i) {
-        auto validator = new QRegularExpressionValidator(i.value(),i.key());
-        i.key()->setValidator(validator);
-    }
+    ui->phoneLineEdit->setValidator(new QRegularExpressionValidator(OPL::RegEx::RX_PHONE_NUMBER, ui->phoneLineEdit));
+    ui->emailLineEdit->setValidator(new QRegularExpressionValidator(OPL::RegEx::RX_EMAIL_ADDRESS, ui->emailLineEdit));
 }
 
 /*!

+ 1 - 1
src/gui/widgets/totalswidget.cpp

@@ -44,7 +44,7 @@ void TotalsWidget::setup(const WidgetType widgetType)
             lineEdit->setFocusPolicy(Qt::FocusPolicy::StrongFocus);
             // set a validator for the TO/LDG line edits, the other ones get validated seperately
             if(lineEdit->objectName().contains(QLatin1String("to")) || lineEdit->objectName().contains(QLatin1String("ldg"))) {
-                lineEdit->setValidator( new QIntValidator(0,  std::numeric_limits<int>::max(), this) );
+                lineEdit->setValidator(new QIntValidator(0, std::numeric_limits<int>::max(), this));
             }
         }
         // initialise m_rowData

+ 9 - 0
src/opl.h

@@ -489,6 +489,15 @@ const inline auto TIME_FORMAT = QStringLiteral("hh:mm");
 
 } // namespace Format
 
+namespace RegEx {
+
+const inline auto RX_PHONE_NUMBER  = QRegularExpression(QStringLiteral("^[+]{0,1}[0-9\\-\\s]+"));
+const inline auto RX_EMAIL_ADDRESS = QRegularExpression(QStringLiteral("\\A[a-z0-9!#$%&'*+/=?^_‘{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_‘{|}~-]+)*@"));
+const inline auto RX_TIME_ENTRY    = QRegularExpression(QStringLiteral("([01]?[0-9]|2[0-3]):?[0-5][0-9]?"));
+const inline auto RX_AIRPORT_CODE  = QRegularExpression(QStringLiteral("[a-zA-Z0-9]{1,4}"));
+
+} // namespace RegEx
+
 } // namespace opl
 
 #endif // OPLCONSTANTS_H