Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision b7f1ce8e

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID b7f1ce8e6f426be25ee4172dcc6b9f010032b234
  • Vorgänger 0ec1cbfb
  • Nachfolger e80feccc

validator stuff

Unterschiede anzeigen:

src/validators/lengthvalidator.cpp
#include "lengthvalidator.h"
using namespace CuteEntityManager;
LengthValidator::LengthValidator() : Validator() {
}
ErrorMsg LengthValidator::validateParam(QVariant value, Param param) const {
QString val = value.toString();
if (param.getName() == "min" && val.length() < param.getValue().toLongLong()) {
return ErrorMsg(param.getName(),
"<property> must have more than " + param.getValue().toString() +
" characters.");
} else if (param.getName() == "max"
&& val.length() > param.getValue().toLongLong() ) {
return ErrorMsg(param.getName(),
"<property> must have more than " + param.getValue().toString() +
"characters.");
}
return ErrorMsg();
}
src/validators/lengthvalidator.h
#ifndef LENGTHVALIDATOR_H
#define LENGTHVALIDATOR_H
#include "validator.h"
namespace CuteEntityManager {
class LengthValidator : public Validator {
Q_OBJECT
public:
LengthValidator();
protected:
ErrorMsg validateParam(QVariant value, Param param) const final override;
};
}
#endif // LENGTHVALIDATOR_H
src/validators/requiredvalidator.cpp
}
ErrorMsg RequiredValidator::validateParam(QVariant value, Param param) const {
if (value.isNull()) {
if (value.isNull() || value.toString().isEmpty()) {
return ErrorMsg(param.getName(), "Value is not set");
}
return ErrorMsg();
src/validators/validator.cpp
const QHash<QString, QString> Validator::builtInValidators() {
QHash<QString, QString> hash = QHash<QString, QString>();
hash.insert("compare", "CompareValidator");
hash.insert("default", "DefaultValidator");
hash.insert("email", "EmailValidator");
hash.insert("exists", "ExistValidator");
hash.insert("file", "FileValidator");
hash.insert("image", "ImageValidator");
hash.insert("number", "NumberValidator");
hash.insert("date", "DateValidator");
hash.insert("required", "RequiredValidator");
hash.insert("unique", "UniqueValidator");
hash.insert("url", "UrlValidator");
hash.insert("pattern", "PatternValidator");
hash.insert("compare", "CuteEntityManager::CompareValidator");
hash.insert("default", "CuteEntityManager::DefaultValidator");
hash.insert("email", "CuteEntityManager::EmailValidator");
hash.insert("exists", "CuteEntityManager::ExistValidator");
hash.insert("file", "CuteEntityManager::FileValidator");
hash.insert("image", "CuteEntityManager::ImageValidator");
hash.insert("number", "CuteEntityManager::NumberValidator");
hash.insert("date", "CuteEntityManager::DateValidator");
hash.insert("required", "CuteEntityManager::RequiredValidator");
hash.insert("unique", "CuteEntityManager::UniqueValidator");
hash.insert("url", "CuteEntityManager::UrlValidator");
hash.insert("pattern", "CuteEntityManager::PatternValidator");
hash.insert("length", "CuteEntityManager::LengthValidator");
return hash;
}
QList<ErrorMsg> Validator::validate(QVariant value, QList<Param> params) const {
QList<ErrorMsg> msgs = QList<ErrorMsg>();
if (params.isEmpty()) {
params.append(Param(""));
}
for (int i = 0; i < params.size(); ++i) {
ErrorMsg msg = this->validateParam(value, params.at(i));
if (!msg.getErrorMsg().isEmpty()) {
src/validators/validatorfactory.cpp
#include "urlvalidator.h"
#include "uniquevalidator.h"
#include "patternvalidator.h"
#include "lengthvalidator.h"
#include <QDebug>
using namespace CuteEntityManager;
ValidatorFactory::ValidatorFactory() {
......
if ( constructor == nullptr ) {
return nullptr;
}
return (*constructor)();
}
QSharedPointer<Validator> ValidatorFactory::getValidatorObject(
const QString &shortname) {
if (!ValidatorFactory::validatorInstances.contains(shortname)) {
ValidatorFactory::registerClasses();
ValidatorFactory::validatorInstances.insert(shortname,
QSharedPointer<Validator>(ValidatorFactory::createValidator(shortname)));
}
......
ValidatorFactory::registerClass<PatternValidator>();
ValidatorFactory::registerClass<UniqueValidator>();
ValidatorFactory::registerClass<UrlValidator>();
ValidatorFactory::registerClass<LengthValidator>();
}
}
src/validators/validatorfactory.h
static Validator *createValidator(const QString &shortname);
template<typename T>
static void registerClass() {
if (!ValidatorFactory::instance.contains(
T::staticMetaObject.className())) {
if (!ValidatorFactory::instance.contains(T::staticMetaObject.className())) {
ValidatorFactory::instance.insert( T::staticMetaObject.className(),
&constructorHelper<T> );
}

Auch abrufbar als: Unified diff