Added support for form validations.

This commit is contained in:
2015-11-10 22:39:58 +01:00
parent 6bc7fd8762
commit 5993193ffb
5 changed files with 59 additions and 6 deletions
+31 -4
View File
@@ -9,6 +9,7 @@
#include <QDebug>
#include "service.h"
#include "ivalidator.h"
template <class T>
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<T> entity) {
m_entity = entity;
@@ -31,9 +37,14 @@ public:
}
}
void registerValidator(IValidator *validator) {
m_validators.append(validator);
}
private:
QSharedPointer<T> m_entity;
QList<QWidget*> m_bindWidgets;
QList<IValidator*> 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<T> entity);
void recordUpdated(QSharedPointer<T> entity);
void validationError(QString errMessage);
public slots:
void saveRecord() {
bindToData();
bool saveRecord() {
if (!bindToData())
{
return false;
}
Service<T> service;
service.save(m_entity);
@@ -65,6 +90,8 @@ public slots:
} else {
emit recordUpdated(m_entity);
}
return true;
}
};