cutelyst 4.3.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-2023 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{
46 Q_D(const ValidatorDifferent);
47
48 Q_UNUSED(errorData);
49
50 const QString _label = label(c);
51 const QString _otherLabel = [d, c]() -> QString {
52 if (d->otherLabel) {
53 return d->translationContext ? c->translate(d->translationContext, d->otherLabel)
54 : c->qtTrId(d->otherLabel);
55 } else {
56 return {};
57 }
58 }();
59
60 if (_label.isEmpty()) {
61 //: %1 will be replaced by the other field’s label or name to compare with
62 //% "Has to be different from the value in the “%1” field."
63 return c->qtTrId("cutelyst-valdifferent-genvalerr")
64 .arg(!_otherLabel.isEmpty() ? _otherLabel : d->otherField);
65 } else {
66 //: %1 will be replaced by the field label, %2 will be replaced by the other field’s label
67 //: to compare with
68 //% "The value in the “%1” field has to be different from the value in the “%2“ field."
69 return c->qtTrId("cutelyst-valdifferent-genvalerr-label")
70 .arg(_label, !_otherLabel.isEmpty() ? _otherLabel : d->otherField);
71 }
72}
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:484
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
Checks if two values are different.
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(Context *c) const
bool trimBefore() const noexcept
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) 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.