Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision e80feccc

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID e80fecccb3257b829fd59b3049c22358a90112c7
  • Vorgänger b7f1ce8e
  • Nachfolger 92efdbef

validator stuff

Unterschiede anzeigen:

EntityManager.pro
src/validators/filevalidator.h \
src/validators/imagevalidator.h \
src/validators/uniquevalidator.h \
src/validators/patternvalidator.h
src/validators/patternvalidator.h \
src/validators/lengthvalidator.h
SOURCES += \
src/entity.cpp \
......
src/validators/filevalidator.cpp \
src/validators/imagevalidator.cpp \
src/validators/uniquevalidator.cpp \
src/validators/patternvalidator.cpp
src/validators/patternvalidator.cpp \
src/validators/lengthvalidator.cpp
CONFIG += c++14
QMAKE_CXXFLAGS += -std=c++14
samples/validators/address.cpp
QList<ValidationRule> Address::validationRules() const {
QList<ValidationRule> rules = QList<ValidationRule>();
rules.append(ValidationRule("required", {"street", "postcode","label"}));
//rules.append(ValidationRule("required"));
rules.append(ValidationRule("required", {"street", "postcode", "city"}));
//German postcodes
rules.append(ValidationRule("number", "postcode", "min", 10000, "max", 99999));
return rules;
}
samples/validators/main.cpp
e->createTable("Person");
e->createTable("Address");
QSharedPointer<Entity> p1 = QSharedPointer<Person>(new Person("Thomas", "Berg",
QSharedPointer<Entity> p1 = QSharedPointer<Person>(new Person("Thomas", "B",
Person::Gender::MALE, "", QString(), QString(), QDate(1971, 7, 13), 0));
QSharedPointer<Entity>p2 = QSharedPointer<Person>(new Person("Teresa", "Conrad",
Person::Gender::FEMALE, "", QString(), QString(), QDate(1970, 7, 13), 0));
......
Person::Gender::MALE, "", QString(), QString(), QDate(1972, 7, 13), 0));
//validation takes also place before save/create/merge
qDebug() << "p1 valid:" << e->validate(p1);
qDebug() << "p2 valid:" << e->validate(p2);
qDebug() << "p3 valid:" << e->validate(p3);
qDebug() << "p1 valid:" << e->validate(p1) << p1->getErrorsAsString();
qDebug() << "p2 valid:" << e->validate(p2) << p2->getErrorsAsString();
qDebug() << "p3 valid:" << e->validate(p3) << p3->getErrorsAsString();
QSharedPointer<Entity> a1 = QSharedPointer<Address>(new Address("",
"Mentzelstraße 327",
"33617", "Bielefeld"));
QSharedPointer<Entity> a2 = QSharedPointer<Address>(new Address("Erzieher",
"Bundesallee 252",
"49082", "Osnabrück"));
qDebug() << "a1 valid:" << e->validate(a1);
qDebug() << "a2 valid:" << e->validate(a2);
"4908", "Osnabrück"));
qDebug() << "a1 valid:" << e->validate(a1) << a1->getErrorsAsString();
qDebug() << "a2 valid:" << e->validate(a2) << a2->getErrorsAsString();
qDebug() << a2->getErrors().size();
return 0;
}
samples/validators/person.cpp
}
QList<ValidationRule> Person::validationRules() const {
return Entity::validationRules();
QList<ValidationRule> rules = QList<ValidationRule>();
rules.append(ValidationRule("length", {"firstName", "familyName"}, "min", 2));
return rules;
}
QString Person::fullName(NameOrder nameOrder) const {
src/entity.cpp
return errors;
}
QString Entity::getErrorsAsString() const {
QList<ErrorMsg> errors = this->getErrors();
bool first = true;
QString r = "";
for (int i = 0; i < errors.size(); ++i) {
ErrorMsg msg = errors.at(i);
if (first) {
first = false;
} else {
r += ";";
}
r += msg.getErrorMsg();
}
return r;
}
void Entity::setErrors(const QList<ErrorMsg> &value) {
errors = value;
this->errors = value;
}
Entity::~Entity() {
src/entity.h
bool hasErrors() const;
QList<ErrorMsg> getErrors() const;
QString getErrorsAsString() const;
void setErrors(const QList<ErrorMsg> &value);
protected:
src/entitymanager.cpp
ValidationRule rule = rules.at(i);
QSharedPointer<Validator> validator = ValidatorFactory::getValidatorObject(
rule.getValidatorName());
qDebug() << "VALIDATOR" << validator;
if (validator) {
for (int var = 0; var < rule.getAttributes().size(); ++var) {
QString attr = rule.getAttributes().at(var);
list.append(validator->validate(entity->getProperty(attr), rule.getParams()));
QList<ErrorMsg> msgs = validator->validate(entity->getProperty(attr),
rule.getParams());
for (int i = 0; i < msgs.size(); ++i) {
QString emsg = msgs.at(i).getErrorMsg().replace("<property>", attr);
ErrorMsg m = msgs.at(i);
m.setErrorMsg(emsg);
list.append(m);
}
}
}
}
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()) {
src/validators/validator.cpp
#include "validator.h"
#include "validatorfactory.h"
#include <QDebug>
using namespace CuteEntityManager;
Validator::Validator() : QObject() {
ValidatorFactory::registerClasses();

Auch abrufbar als: Unified diff