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
8
using namespace
Cutelyst
;
9
10
ValidatorDifferent::ValidatorDifferent
(
const
QString
&field,
11
const
QString
&other,
12
const
char
*otherLabel,
13
const
Cutelyst::ValidatorMessages
&messages)
14
:
ValidatorRule
(*new ValidatorDifferentPrivate(field, other, otherLabel, messages))
15
{
16
}
17
18
ValidatorDifferent::~ValidatorDifferent
() =
default
;
19
20
ValidatorReturnType
ValidatorDifferent::validate
(
Context
*c,
const
ParamsMultiMap
¶ms)
const
21
{
22
ValidatorReturnType
result;
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
44
QString
ValidatorDifferent::genericValidationError
(
Context
*c,
const
QVariant
&errorData)
const
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
}
Cutelyst::Context
The Cutelyst Context.
Definition
context.h:42
Cutelyst::Context::translate
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition
context.cpp:484
Cutelyst::Context::qtTrId
QString qtTrId(const char *id, int n=-1) const
Definition
context.h:656
Cutelyst::ValidatorDifferent
Checks if two values are different.
Definition
validatordifferent.h:39
Cutelyst::ValidatorDifferent::ValidatorDifferent
ValidatorDifferent(const QString &field, const QString &other, const char *otherLabel=nullptr, const ValidatorMessages &messages=ValidatorMessages())
Definition
validatordifferent.cpp:10
Cutelyst::ValidatorDifferent::~ValidatorDifferent
~ValidatorDifferent() override
Cutelyst::ValidatorDifferent::validate
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Definition
validatordifferent.cpp:20
Cutelyst::ValidatorDifferent::genericValidationError
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Definition
validatordifferent.cpp:44
Cutelyst::ValidatorRule
Base class for all validator rules.
Definition
validatorrule.h:304
Cutelyst::ValidatorRule::validationError
QString validationError(Context *c, const QVariant &errorData={}) const
Definition
validatorrule.cpp:61
Cutelyst::ValidatorRule::label
QString label(Context *c) const
Definition
validatorrule.cpp:49
Cutelyst::ValidatorRule::trimBefore
bool trimBefore() const noexcept
Definition
validatorrule.cpp:165
Cutelyst::ValidatorRule::value
QString value(const ParamsMultiMap ¶ms) const
Definition
validatorrule.cpp:34
Cutelyst::ValidatorRule::debugString
QString debugString(Context *c) const
Definition
validatorrule.cpp:157
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition
group-core-actions.dox:1
QMultiMap
QMultiMap::value
T value(const Key &key, const T &defaultValue) const const
QString
QString::arg
QString arg(Args &&... args) const const
QString::isEmpty
bool isEmpty() const const
QVariant
QVariant::setValue
void setValue(QVariant &&value)
Cutelyst::ValidatorMessages
Stores custom error messages and the input field label.
Definition
validatorrule.h:142
Cutelyst::ValidatorReturnType
Contains the result of a single input parameter validation.
Definition
validatorrule.h:49
Cutelyst::ValidatorReturnType::value
QVariant value
Definition
validatorrule.h:54
Cutelyst::ValidatorReturnType::errorMessage
QString errorMessage
Definition
validatorrule.h:50
Cutelyst
Plugins
Utils
Validator
validatordifferent.cpp
Generated by
1.9.8