From 5993193ffb30c1ffe25aa6f14061bdf3c708c9e9 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 10 Nov 2015 22:39:58 +0100 Subject: [PATCH 1/2] Added support for form validations. --- core/autoform.h | 35 +++++++++++++++++++++++++++++++---- core/core.pro | 7 +++++-- core/emptystringvalidator.cpp | 7 +++++++ core/emptystringvalidator.h | 11 +++++++++++ core/ivalidator.h | 5 +++++ 5 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 core/emptystringvalidator.cpp create mode 100644 core/emptystringvalidator.h create mode 100644 core/ivalidator.h diff --git a/core/autoform.h b/core/autoform.h index c985615..69410d5 100644 --- a/core/autoform.h +++ b/core/autoform.h @@ -9,6 +9,7 @@ #include #include "service.h" +#include "ivalidator.h" template class AutoForm : public QWidget @@ -18,7 +19,12 @@ public: m_newRec = false; } - virtual ~AutoForm() {} + virtual ~AutoForm() { + foreach (IValidator *val, m_validators) { + delete val; + m_validators.clear(); + } + } void setEntity(QSharedPointer entity) { m_entity = entity; @@ -31,9 +37,14 @@ public: } } + void registerValidator(IValidator *validator) { + m_validators.append(validator); + } + private: QSharedPointer m_entity; QList m_bindWidgets; + QList m_validators; bool m_newRec; void bindToUi() { @@ -43,20 +54,34 @@ private: } } - void bindToData() { + bool bindToData() { + foreach (IValidator *val, m_validators) { + if (!val->validate()) { + emit validationError(val->errMessage()); + return false; + } + } + foreach (QWidget *widget, m_bindWidgets) { const char* prop = widget->metaObject()->userProperty().name(); ((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop)); } + + return true; } signals: void recordAdded(QSharedPointer entity); void recordUpdated(QSharedPointer entity); + void validationError(QString errMessage); public slots: - void saveRecord() { - bindToData(); + bool saveRecord() { + if (!bindToData()) + { + return false; + } + Service service; service.save(m_entity); @@ -65,6 +90,8 @@ public slots: } else { emit recordUpdated(m_entity); } + + return true; } }; diff --git a/core/core.pro b/core/core.pro index 5b20f54..9fdb1d0 100644 --- a/core/core.pro +++ b/core/core.pro @@ -15,7 +15,8 @@ SOURCES += \ data/user.cpp \ context.cpp \ imetadataplugin.cpp \ - transaction.cpp + transaction.cpp \ + emptystringvalidator.cpp HEADERS += core.h\ core_global.h \ @@ -26,7 +27,9 @@ HEADERS += core.h\ imetadataplugin.h \ autotablemodel.h \ autoform.h \ - transaction.h + transaction.h \ + ivalidator.h \ + emptystringvalidator.h unix { target.path = /usr/lib diff --git a/core/emptystringvalidator.cpp b/core/emptystringvalidator.cpp new file mode 100644 index 0000000..67e3926 --- /dev/null +++ b/core/emptystringvalidator.cpp @@ -0,0 +1,7 @@ +#include "emptystringvalidator.h" + +EmptyStringValidator::EmptyStringValidator() +{ + +} + diff --git a/core/emptystringvalidator.h b/core/emptystringvalidator.h new file mode 100644 index 0000000..887d0d7 --- /dev/null +++ b/core/emptystringvalidator.h @@ -0,0 +1,11 @@ +#ifndef EMPTYSTRINGVALIDATOR_H +#define EMPTYSTRINGVALIDATOR_H + + +class EmptyStringValidator : public IValidator +{ +public: + EmptyStringValidator(); +}; + +#endif // EMPTYSTRINGVALIDATOR_H diff --git a/core/ivalidator.h b/core/ivalidator.h new file mode 100644 index 0000000..ebb945b --- /dev/null +++ b/core/ivalidator.h @@ -0,0 +1,5 @@ +#ifndef IVALIDATOR_H +#define IVALIDATOR_H + +#endif // IVALIDATOR_H + From 21ffb16151fda820b45e7217a5f540e29f06cd7c Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Tue, 10 Nov 2015 22:52:04 +0100 Subject: [PATCH 2/2] Forgotten modifications. --- core/emptystringvalidator.cpp | 17 ++++++++++++++++- core/emptystringvalidator.h | 8 +++++++- core/ivalidator.h | 26 ++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/core/emptystringvalidator.cpp b/core/emptystringvalidator.cpp index 67e3926..f7a121d 100644 --- a/core/emptystringvalidator.cpp +++ b/core/emptystringvalidator.cpp @@ -1,7 +1,22 @@ #include "emptystringvalidator.h" -EmptyStringValidator::EmptyStringValidator() +#include +#include + +EmptyStringValidator::EmptyStringValidator(QWidget *widget, const QString &errMessage) + :IValidator(widget, errMessage) +{ +} + +bool EmptyStringValidator::validate() { + QString data = m_widget->property(m_widget->metaObject()->userProperty().name()).toString(); + if (data.isEmpty()) + { + m_widget->setFocus(); + return false; + } + return true; } diff --git a/core/emptystringvalidator.h b/core/emptystringvalidator.h index 887d0d7..c060f86 100644 --- a/core/emptystringvalidator.h +++ b/core/emptystringvalidator.h @@ -1,11 +1,17 @@ #ifndef EMPTYSTRINGVALIDATOR_H #define EMPTYSTRINGVALIDATOR_H +#include +#include "ivalidator.h" class EmptyStringValidator : public IValidator { public: - EmptyStringValidator(); + EmptyStringValidator(QWidget *widget, const QString &errMessage); + + // IValidator interface +public: + bool validate(); }; #endif // EMPTYSTRINGVALIDATOR_H diff --git a/core/ivalidator.h b/core/ivalidator.h index ebb945b..d096a7d 100644 --- a/core/ivalidator.h +++ b/core/ivalidator.h @@ -1,5 +1,31 @@ #ifndef IVALIDATOR_H #define IVALIDATOR_H +#include +#include + +class IValidator +{ +public: + IValidator(QWidget *widget, const QString &errMessage) + { + m_widget = widget; + m_errMessage = errMessage; + } + + virtual bool validate() = 0; + + QString errMessage() + { + return m_errMessage; + } + +protected: + QWidget *m_widget; + +private: + QString m_errMessage; +}; + #endif // IVALIDATOR_H