Added support for form validations.
This commit is contained in:
+31
-4
@@ -9,6 +9,7 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
#include "service.h"
|
#include "service.h"
|
||||||
|
#include "ivalidator.h"
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
class AutoForm : public QWidget
|
class AutoForm : public QWidget
|
||||||
@@ -18,7 +19,12 @@ public:
|
|||||||
m_newRec = false;
|
m_newRec = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~AutoForm() {}
|
virtual ~AutoForm() {
|
||||||
|
foreach (IValidator *val, m_validators) {
|
||||||
|
delete val;
|
||||||
|
m_validators.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void setEntity(QSharedPointer<T> entity) {
|
void setEntity(QSharedPointer<T> entity) {
|
||||||
m_entity = entity;
|
m_entity = entity;
|
||||||
@@ -31,9 +37,14 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void registerValidator(IValidator *validator) {
|
||||||
|
m_validators.append(validator);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QSharedPointer<T> m_entity;
|
QSharedPointer<T> m_entity;
|
||||||
QList<QWidget*> m_bindWidgets;
|
QList<QWidget*> m_bindWidgets;
|
||||||
|
QList<IValidator*> m_validators;
|
||||||
bool m_newRec;
|
bool m_newRec;
|
||||||
|
|
||||||
void bindToUi() {
|
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) {
|
foreach (QWidget *widget, m_bindWidgets) {
|
||||||
const char* prop = widget->metaObject()->userProperty().name();
|
const char* prop = widget->metaObject()->userProperty().name();
|
||||||
((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop));
|
((QObject*)m_entity.data())->setProperty(widget->objectName().toStdString().c_str(), widget->property(prop));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void recordAdded(QSharedPointer<T> entity);
|
void recordAdded(QSharedPointer<T> entity);
|
||||||
void recordUpdated(QSharedPointer<T> entity);
|
void recordUpdated(QSharedPointer<T> entity);
|
||||||
|
void validationError(QString errMessage);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void saveRecord() {
|
bool saveRecord() {
|
||||||
bindToData();
|
if (!bindToData())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Service<T> service;
|
Service<T> service;
|
||||||
service.save(m_entity);
|
service.save(m_entity);
|
||||||
|
|
||||||
@@ -65,6 +90,8 @@ public slots:
|
|||||||
} else {
|
} else {
|
||||||
emit recordUpdated(m_entity);
|
emit recordUpdated(m_entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+5
-2
@@ -15,7 +15,8 @@ SOURCES += \
|
|||||||
data/user.cpp \
|
data/user.cpp \
|
||||||
context.cpp \
|
context.cpp \
|
||||||
imetadataplugin.cpp \
|
imetadataplugin.cpp \
|
||||||
transaction.cpp
|
transaction.cpp \
|
||||||
|
emptystringvalidator.cpp
|
||||||
|
|
||||||
HEADERS += core.h\
|
HEADERS += core.h\
|
||||||
core_global.h \
|
core_global.h \
|
||||||
@@ -26,7 +27,9 @@ HEADERS += core.h\
|
|||||||
imetadataplugin.h \
|
imetadataplugin.h \
|
||||||
autotablemodel.h \
|
autotablemodel.h \
|
||||||
autoform.h \
|
autoform.h \
|
||||||
transaction.h
|
transaction.h \
|
||||||
|
ivalidator.h \
|
||||||
|
emptystringvalidator.h
|
||||||
|
|
||||||
unix {
|
unix {
|
||||||
target.path = /usr/lib
|
target.path = /usr/lib
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
#include "emptystringvalidator.h"
|
||||||
|
|
||||||
|
EmptyStringValidator::EmptyStringValidator()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef EMPTYSTRINGVALIDATOR_H
|
||||||
|
#define EMPTYSTRINGVALIDATOR_H
|
||||||
|
|
||||||
|
|
||||||
|
class EmptyStringValidator : public IValidator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
EmptyStringValidator();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // EMPTYSTRINGVALIDATOR_H
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#ifndef IVALIDATOR_H
|
||||||
|
#define IVALIDATOR_H
|
||||||
|
|
||||||
|
#endif // IVALIDATOR_H
|
||||||
|
|
||||||
Reference in New Issue
Block a user