Added new validator, userform validation

print
Zdenek Jonak 9 years ago
parent 5dbebcc947
commit 6f78308d7d

@ -25,8 +25,8 @@ public:
virtual ~AutoForm() { virtual ~AutoForm() {
foreach (IValidator *val, m_validators) { foreach (IValidator *val, m_validators) {
delete val; delete val;
m_validators.clear();
} }
m_validators.clear();
} }
void setEntity(QSharedPointer<T> entity) { void setEntity(QSharedPointer<T> entity) {

@ -38,7 +38,8 @@ SOURCES += \
roles/rolesform.cpp \ roles/rolesform.cpp \
permissionservice.cpp \ permissionservice.cpp \
filterui.cpp \ filterui.cpp \
exprevaluator.cpp exprevaluator.cpp \
samestringvalidator.cpp
HEADERS += core.h\ HEADERS += core.h\
core_global.h \ core_global.h \
@ -73,7 +74,8 @@ HEADERS += core.h\
roles/rolesform.h \ roles/rolesform.h \
permissionservice.h \ permissionservice.h \
filterui.h \ filterui.h \
exprevaluator.h exprevaluator.h \
samestringvalidator.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib

@ -1,5 +1,6 @@
#include "formdialog.h" #include "formdialog.h"
#include "ui_formdialog.h" #include "ui_formdialog.h"
#include <QMessageBox>
FormDialog::FormDialog(QWidget *parent) : FormDialog::FormDialog(QWidget *parent) :
QDialog(parent), QDialog(parent),
@ -21,11 +22,20 @@ void FormDialog::setForm(IForm *formWidget)
{ {
ui->verticalLayout->addWidget(formWidget); ui->verticalLayout->addWidget(formWidget);
m_form = formWidget; m_form = formWidget;
connect(m_form, SIGNAL(validationError(QString)), this, SLOT(onValidationError(QString)));
setGeometry(formWidget->geometry()); setGeometry(formWidget->geometry());
} }
} }
void FormDialog::on_buttonBox_accepted() void FormDialog::onValidationError(const QString &message)
{ {
m_form->saveRecord(); QMessageBox::critical(this, tr("Validation error"), tr(message.toStdString().c_str()));
}
void FormDialog::accept()
{
if (m_form->saveRecord())
{
QDialog::accept();
}
} }

@ -20,13 +20,17 @@ public:
void setForm(IForm *formWidget); void setForm(IForm *formWidget);
private slots:
void on_buttonBox_accepted();
private: private:
bool m_formSet; bool m_formSet;
IForm *m_form; IForm *m_form;
Ui::FormDialog *ui; Ui::FormDialog *ui;
private slots:
void onValidationError(const QString &message);
// QDialog interface
public slots:
void accept();
}; };
#endif // FORMDIALOG_H #endif // FORMDIALOG_H

@ -16,7 +16,7 @@ public:
signals: signals:
void recordAdded(); void recordAdded();
void recordUpdated(); void recordUpdated();
void validationError(QString errMessage); void validationError(const QString &errMessage);
public slots: public slots:
virtual bool saveRecord() = 0; virtual bool saveRecord() = 0;

@ -0,0 +1,21 @@
#include "samestringvalidator.h"
#include <QMetaProperty>
SameStringValidator::SameStringValidator(QWidget *widget_1, QWidget * widget_2, const QString & errorMessage)
:IValidator(widget_1,errorMessage)
{
m_widget_2 = widget_2;
}
bool SameStringValidator::validate()
{
QString data = m_widget->property(m_widget->metaObject()->userProperty().name()).toString();
QString data_2 = m_widget_2->property(m_widget_2->metaObject()->userProperty().name()).toString();
if (data != data_2)
{
m_widget->setFocus();
return false;
}
return true;
}

@ -0,0 +1,15 @@
#ifndef SAMESTRINGVALIDATOR_H
#define SAMESTRINGVALIDATOR_H
#include "ivalidator.h"
#include "core_global.h"
class CORESHARED_EXPORT SameStringValidator : public IValidator
{
public:
SameStringValidator(QWidget * widget_1, QWidget *widget_2, const QString &errorMessage);
bool validate();
private:
QWidget * m_widget_2;
};
#endif // SAMESTRINGVALIDATOR_H

@ -5,6 +5,8 @@
#include <QHeaderView> #include <QHeaderView>
#include "../data/core-data.h" #include "../data/core-data.h"
#include "../service.h" #include "../service.h"
#include "../emptystringvalidator.h"
#include "../samestringvalidator.h"
UserForm::UserForm(QWidget *parent) : UserForm::UserForm(QWidget *parent) :
AutoForm<User>(parent), AutoForm<User>(parent),
@ -16,6 +18,15 @@ UserForm::UserForm(QWidget *parent) :
registerBinding(ui->name); registerBinding(ui->name);
registerBinding(ui->isAdmin); registerBinding(ui->isAdmin);
registerBinding(ui->active); registerBinding(ui->active);
EmptyStringValidator * esv_login = new EmptyStringValidator(ui->login,"Enter Login Name");
registerValidator(esv_login);
EmptyStringValidator * esv_password = new EmptyStringValidator(ui->password,"Enter Password");
registerValidator(esv_password);
EmptyStringValidator * esv_name = new EmptyStringValidator(ui->name,"Enter Name");
registerValidator(esv_name);
SameStringValidator * ssv_password = new SameStringValidator(ui->password,ui->retypePassword,"Passwords doesen't match");
registerValidator(ssv_password);
} }
UserForm::~UserForm() UserForm::~UserForm()
@ -25,6 +36,8 @@ UserForm::~UserForm()
void UserForm::bindOtherToUi() void UserForm::bindOtherToUi()
{ {
ui->retypePassword->setText(this->entity()->password());
Service<Role> srv; Service<Role> srv;
QList<QSharedPointer<Role> > roles = this->entity()->listRoles(); QList<QSharedPointer<Role> > roles = this->entity()->listRoles();
ui->tableWidget->setRowCount(srv.all().count()); ui->tableWidget->setRowCount(srv.all().count());

@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>458</width>
<height>300</height> <height>301</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
@ -32,19 +32,23 @@
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QLineEdit" name="password"/> <widget class="QLineEdit" name="password">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item> </item>
<item row="2" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Name</string> <string>Name</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="3" column="1">
<widget class="QLineEdit" name="name"/> <widget class="QLineEdit" name="name"/>
</item> </item>
<item row="4" column="1"> <item row="5" column="1">
<widget class="QCheckBox" name="isAdmin"> <widget class="QCheckBox" name="isAdmin">
<property name="layoutDirection"> <property name="layoutDirection">
<enum>Qt::RightToLeft</enum> <enum>Qt::RightToLeft</enum>
@ -54,7 +58,7 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="5" column="1"> <item row="6" column="1">
<widget class="QCheckBox" name="active"> <widget class="QCheckBox" name="active">
<property name="layoutDirection"> <property name="layoutDirection">
<enum>Qt::RightToLeft</enum> <enum>Qt::RightToLeft</enum>
@ -64,18 +68,41 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="4" column="1">
<widget class="QTableWidget" name="tableWidget"/> <widget class="QTableWidget" name="tableWidget"/>
</item> </item>
<item row="3" column="0"> <item row="4" column="0">
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="label_4">
<property name="text"> <property name="text">
<string>Roles</string> <string>Roles</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QLineEdit" name="retypePassword">
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Retype Password</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<tabstops>
<tabstop>login</tabstop>
<tabstop>password</tabstop>
<tabstop>retypePassword</tabstop>
<tabstop>name</tabstop>
<tabstop>tableWidget</tabstop>
<tabstop>isAdmin</tabstop>
<tabstop>active</tabstop>
</tabstops>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

Loading…
Cancel
Save