cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorresult.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorresult_p.h"
7
8#include <QJsonArray>
9#include <QJsonValue>
10
11using namespace Cutelyst;
12
14 : d(new ValidatorResultPrivate)
15{
16}
17
19 : d(other.d)
20{
21}
22
24{
25 d = other.d;
26 return *this;
27}
28
32
34{
35 return d->errors.empty();
36}
37
38void ValidatorResult::addError(const QString &field, const QString &message)
39{
40 QStringList fieldErrors = d->errors.value(field);
41 fieldErrors.append(message);
42 d->errors.insert(field, fieldErrors);
43}
44
46{
47 QStringList strings;
48
49 auto i = d->errors.constBegin();
50 while (i != d->errors.constEnd()) {
51 strings.append(i.value());
52 ++i;
53 }
54
55 return strings;
56}
57
59{
60 return d->errors;
61}
62
64{
65 return d->errors.value(field);
66}
67
68bool ValidatorResult::hasErrors(const QString &field) const
69{
70 return d->errors.contains(field);
71}
72
74{
75 QJsonObject json;
76
77 if (!d->errors.empty()) {
78 auto i = d->errors.constBegin();
79 while (i != d->errors.constEnd()) {
80 json.insert(i.key(), QJsonValue(QJsonArray::fromStringList(i.value())));
81 ++i;
82 }
83 }
84
85 return json;
86}
87
89{
90 return QStringList(d->errors.keys());
91}
92
93QVariantHash ValidatorResult::values() const
94{
95 return d->values;
96}
97
99{
100 return d->values.value(field);
101}
102
104{
105 d->values.insert(field, value);
106}
107
108QVariantHash ValidatorResult::extras() const
109{
110 return d->extras;
111}
112
114{
115 return d->extras.value(field);
116}
117
119{
120 d->extras.insert(field, extra);
121}
void addExtra(const QString &field, const QVariant &extra)
Adds new extra data that came up when validating the input field.
QStringList failedFields() const
Returns a list of fields with errors.
QJsonObject errorsJsonObject() const
Returns the dictionray containing fields with errors as JSON object.
QVariant value(const QString &field) const
Returns the extracted value for the input field.
void addValue(const QString &field, const QVariant &value)
Adds a new value extracted from the specified input field.
QHash< QString, QStringList > errors() const
Returns a dictionary containing fields with errors.
QVariantHash values() const
Returns the values that have been extracted by the validators.
~ValidatorResult()
Deconstructs the ValidatorResult.
QVariant extra(const QString &field) const
Returns the extra data for the input field.
ValidatorResult & operator=(const ValidatorResult &other)
Assigns other to this ValidatorResult.
bool isValid() const
Returns true if the validation was successful.
void addError(const QString &field, const QString &message)
Adds new error information to the internal QHash.
ValidatorResult()
Constructs a new ValidatorResult.
QStringList errorStrings() const
Returns a list of all error messages.
QVariantHash extras() const
Returns all extra data that has been extracted by the validators.
bool hasErrors(const QString &field) const
Returns true if the field has validation errors.
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QJsonArray fromStringList(const QStringList &list)
iterator insert(const QString &key, const QJsonValue &value)
void append(const T &value)
const_iterator constBegin() const const
T value(int i) const const
T value() const const