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