cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredwith.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorrequiredwith_p.h"
7
8using namespace Cutelyst;
9
11 const QStringList &otherFields,
12 const Cutelyst::ValidatorMessages &messages)
13 : ValidatorRule(*new ValidatorRequiredWithPrivate(field, otherFields, messages))
14{
15}
16
20
22{
24
25 Q_D(const ValidatorRequiredWith);
26
27 if (d->otherFields.empty()) {
29 qCWarning(C_VALIDATOR,
30 "ValidatorRequiredWith: invalid validation data for field %s at %s::%s",
31 qPrintable(field()),
32 qPrintable(c->controllerName()),
33 qPrintable(c->actionName()));
34 } else {
35 bool containsOther = false;
36 const QString v = value(params);
37
38 const QStringList ofc = d->otherFields;
39
40 for (const QString &other : ofc) {
41 if (params.contains(other)) {
42 containsOther = true;
43 break;
44 }
45 }
46
47 if (containsOther) {
48 if (!v.isEmpty()) {
49 result.value.setValue(v);
50 } else {
51 result.errorMessage = validationError(c);
52 qCDebug(C_VALIDATOR,
53 "ValidatorRequiredWith: Validation failed for field %s at %s::%s",
54 qPrintable(field()),
55 qPrintable(c->controllerName()),
56 qPrintable(c->actionName()));
57 }
58 } else {
59 if (!v.isEmpty()) {
60 result.value.setValue(v);
61 }
62 }
63 }
64
65 return result;
66}
67
69{
70 QString error;
71 Q_UNUSED(errorData)
72 const QString _label = label(c);
73 if (_label.isEmpty()) {
74 error = c->translate("Cutelyst::ValidatorRequiredWith", "This is required.");
75 } else {
76 //: %1 will be replaced by the field label
77 error = c->translate("Cutelyst::ValidatorRequiredWith", "The “%1” field is required.")
78 .arg(_label);
79 }
80 return error;
81}
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
~ValidatorRequiredWith() override
Deconstructs the required with validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
ValidatorRequiredWith(const QString &field, const QStringList &otherFields, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required with 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.