cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredwithall.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorrequiredwithall_p.h"
7
8using namespace Cutelyst;
9
11 const QStringList &otherFields,
12 const ValidatorMessages &messages)
13 : ValidatorRule(*new ValidatorRequiredWithAllPrivate(field, otherFields, messages))
14{
15}
16
20
22 const ParamsMultiMap &params) const
23{
25
26 Q_D(const ValidatorRequiredWithAll);
27
28 if (d->otherFields.empty()) {
30 qCWarning(C_VALIDATOR,
31 "ValidatorRequiredWithAll: invalid validation data for field %s at %s::%s",
32 qPrintable(field()),
33 qPrintable(c->controllerName()),
34 qPrintable(c->actionName()));
35 } else {
36
37 bool containsAll = true;
38
39 const QStringList ofc = d->otherFields;
40
41 for (const QString &other : ofc) {
42 if (!params.contains(other)) {
43 containsAll = false;
44 break;
45 }
46 }
47
48 const QString v = value(params);
49
50 if (containsAll) {
51 if (!v.isEmpty()) {
52 result.value.setValue(v);
53 } else {
54 result.errorMessage = validationError(c);
55 qCDebug(C_VALIDATOR,
56 "ValidatorRequiredWithAll: Validation failed for field %s at %s::%s",
57 qPrintable(field()),
58 qPrintable(c->controllerName()),
59 qPrintable(c->actionName()));
60 }
61 } else {
62 if (!v.isEmpty()) {
63 result.value.setValue(v);
64 }
65 }
66 }
67
68 return result;
69}
70
72 const QVariant &errorData) const
73{
74 QString error;
75 const QString _label = label(c);
76 Q_UNUSED(errorData)
77 if (_label.isEmpty()) {
78 error = c->translate("Cutelyst::ValidatorRequiredWithAll", "This is required.");
79 } else {
80 //: %1 will be replaced by the field label
81 error = c->translate("Cutelyst::ValidatorRequiredWithAll", "The “%1” field is required.")
82 .arg(_label);
83 }
84 return error;
85}
The Cutelyst Context.
Definition context.h:39
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:490
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorRequiredWithAll(const QString &field, const QStringList &otherFields, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required with all validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
~ValidatorRequiredWithAll() override
Deconstructs the required with all validator.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
bool contains(const Key &key, const T &value) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
void setValue(const T &value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.