cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequiredunlessstash.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2018-2022 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatorrequiredunlessstash_p.h"
7
8using namespace Cutelyst;
9
11 const QString &stashKey,
12 const QVariantList &stashValues,
13 const ValidatorMessages &messages)
15 *new ValidatorRequiredUnlessStashPrivate(field, stashKey, stashValues, messages))
16{
17}
18
22
24 const ParamsMultiMap &params) const
25{
27
29
30 if (d->stashKey.isEmpty() || d->stashValues.empty()) {
32 qCWarning(C_VALIDATOR,
33 "ValidatorRequiredUnlessStash: invalid validation data for field %s at %s::%s",
34 qPrintable(field()),
35 qPrintable(c->controllerName()),
36 qPrintable(c->actionName()));
37 } else {
38 const QString v = value(params);
39 const QVariant sv = c->stash(d->stashKey);
40 if (!d->stashValues.contains(sv)) {
41 if (!v.isEmpty()) {
42 result.value.setValue(v);
43 } else {
44 result.errorMessage = validationError(c);
45 }
46 } else {
47 if (!v.isEmpty()) {
48 result.value.setValue(v);
49 }
50 }
51 }
52
53 return result;
54}
55
57 const QVariant &errorData) const
58{
59 QString error;
60 Q_UNUSED(errorData)
61 const QString _label = label(c);
62 if (_label.isEmpty()) {
63 error = c->translate("Cutelyst::ValidatorRequiredUnlessStash", "This is required.");
64 } else {
65 //: %1 will be replaced by the field label
66 error =
67 c->translate("Cutelyst::ValidatorRequiredUnlessStash", "The “%1” field is required.")
68 .arg(_label);
69 }
70 return error;
71}
The Cutelyst Context.
Definition context.h:39
void stash(const QVariantHash &unite)
Definition context.cpp:566
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.
ValidatorRequiredUnlessStash(const QString &field, const QString &stashKey, const QVariantList &stashValues, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required unless stash validator.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
~ValidatorRequiredUnlessStash() override
Deconstructs the required unless stash 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
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.