cutelyst  3.7.0
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 #include <QJsonValue>
8 #include <QJsonArray>
9 
10 using namespace Cutelyst;
11 
13  d(new ValidatorResultPrivate)
14 {
15 }
16 
18  d(other.d)
19 {
20 }
21 
23 {
24  d = other.d;
25  return *this;
26 }
27 
29 {
30 }
31 
33 {
34  return d->errors.empty();
35 }
36 
37 void ValidatorResult::addError(const QString &field, const QString &message)
38 {
39  QStringList fieldErrors = d->errors.value(field);
40  fieldErrors.append(message);
41  d->errors.insert(field, fieldErrors);
42 }
43 
44 QStringList ValidatorResult::errorStrings() const
45 {
46  QStringList strings;
47 
48  auto i = d->errors.constBegin();
49  while (i != d->errors.constEnd()) {
50  strings.append(i.value());
51  ++i;
52  }
53 
54  return strings;
55 }
56 
57 QHash<QString, QStringList> ValidatorResult::errors() const
58 {
59  return d->errors;
60 }
61 
62 bool ValidatorResult::hasErrors(const QString &field) const
63 {
64  return d->errors.contains(field);
65 }
66 
68 {
69  QJsonObject json;
70 
71  if (!d->errors.empty()) {
72  auto i = d->errors.constBegin();
73  while (i != d->errors.constEnd()) {
74  json.insert(i.key(), QJsonValue(QJsonArray::fromStringList(i.value())));
75  ++i;
76  }
77  }
78 
79  return json;
80 }
81 
82 QStringList ValidatorResult::failedFields() const
83 {
84  return QStringList(d->errors.keys());
85 }
86 
87 QVariantHash ValidatorResult::values() const
88 {
89  return d->values;
90 }
91 
92 QVariant ValidatorResult::value(const QString &field) const
93 {
94  return d->values.value(field);
95 }
96 
97 void ValidatorResult::addValue(const QString &field, const QVariant &value)
98 {
99  d->values.insert(field, value);
100 }
101 
102 QVariantHash ValidatorResult::extras() const
103 {
104  return d->extras;
105 }
106 
107 QVariant ValidatorResult::extra(const QString &field) const
108 {
109  return d->extras.value(field);
110 }
111 
112 void ValidatorResult::addExtra(const QString &field, const QVariant &extra)
113 {
114  d->extras.insert(field, extra);
115 }
Provides information about performed validations.
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