cutelyst 5.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatordifferent.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatordifferent_p.h"
7
8using namespace Cutelyst;
9
11 const QString &other,
12 const char *otherLabel,
13 const Cutelyst::ValidatorMessages &messages)
14 : ValidatorRule(*new ValidatorDifferentPrivate(field, other, otherLabel, messages))
15{
16}
17
19
21{
23
24 Q_D(const ValidatorDifferent);
25
26 const QString v = value(params);
27 const QString o =
28 trimBefore() ? params.value(d->otherField).trimmed() : params.value(d->otherField);
29
30 if (!v.isEmpty()) {
31 if ((v == o)) {
32 result.errorMessage = validationError(c);
33 qCDebug(C_VALIDATOR).noquote().nospace()
34 << debugString(c) << " The value in \"" << d->otherField
35 << "\" is not different: \"" << v << "\" == \"" << o << "\"";
36 } else {
37 result.value.setValue(v);
38 }
39 }
40
41 return result;
42}
43
45 const ParamsMultiMap &params,
46 ValidatorRtFn cb) const
47{
48 cb(validate(c, params));
49}
50
52{
53 Q_D(const ValidatorDifferent);
54
55 Q_UNUSED(errorData);
56
57 const QString _label = label(c);
58 const QString _otherLabel = [d, c]() -> QString {
59 if (d->otherLabel) {
60 return d->translationContext ? c->translate(d->translationContext, d->otherLabel)
61 : c->qtTrId(d->otherLabel);
62 } else {
63 return {};
64 }
65 }();
66
67 if (_label.isEmpty()) {
68 //: %1 will be replaced by the other field’s label or name to compare with
69 //% "Has to be different from the value in the “%1” field."
70 return c->qtTrId("cutelyst-valdifferent-genvalerr")
71 .arg(!_otherLabel.isEmpty() ? _otherLabel : d->otherField);
72 } else {
73 //: %1 will be replaced by the field label, %2 will be replaced by the other field’s label
74 //: to compare with
75 //% "The value in the “%1” field has to be different from the value in the “%2“ field."
76 return c->qtTrId("cutelyst-valdifferent-genvalerr-label")
77 .arg(_label, !_otherLabel.isEmpty() ? _otherLabel : d->otherField);
78 }
79}
The Cutelyst Context.
Definition context.h:42
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition context.cpp:485
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
Checks if two values are different.
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
ValidatorDifferent(const QString &field, const QString &other, const char *otherLabel=nullptr, const ValidatorMessages &messages=ValidatorMessages())
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
bool trimBefore() const noexcept
QString debugString(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
QString value(const ParamsMultiMap &params) const
The Cutelyst namespace holds all public Cutelyst API.
T value(const Key &key, const T &defaultValue) const const
QString arg(Args &&... args) const const
bool isEmpty() const const
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.