cutelyst
4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrequired.cpp
1
/*
2
* SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3
* SPDX-License-Identifier: BSD-3-Clause
4
*/
5
6
#include "validatorrequired_p.h"
7
8
using namespace
Cutelyst
;
9
10
ValidatorRequired::ValidatorRequired
(
const
QString
&field,
11
const
Cutelyst::ValidatorMessages
&messages)
12
:
ValidatorRule
(*new ValidatorRequiredPrivate(field, messages))
13
{
14
}
15
16
ValidatorRequired::~ValidatorRequired
() =
default
;
17
18
ValidatorReturnType
ValidatorRequired::validate
(
Cutelyst::Context
*c,
19
const
Cutelyst::ParamsMultiMap
¶ms)
const
20
{
21
ValidatorReturnType
result;
22
23
if
(!params.
contains
(
field
())) {
24
qCDebug(C_VALIDATOR).noquote() <<
debugString
(c) <<
"Field not found"
;
25
result.
errorMessage
=
validationError
(c);
26
return
result;
27
}
28
29
const
QString
v =
value
(params);
30
if
(Q_LIKELY(!v.
isEmpty
())) {
31
result.
value
.
setValue
(v);
32
}
else
{
33
qCDebug(C_VALIDATOR).noquote() <<
debugString
(c) <<
"The field is not present or empty"
;
34
result.
errorMessage
=
validationError
(c);
35
}
36
37
return
result;
38
}
39
40
QString
ValidatorRequired::genericValidationError
(
Cutelyst::Context
*c,
41
const
QVariant
&errorData)
const
42
{
43
Q_UNUSED(errorData)
44
const
QString
_label =
label
(c);
45
if
(_label.
isEmpty
()) {
46
//% "This field is required."
47
return
c->
qtTrId
(
"cutelyst-validator-genvalerr-req"
);
48
}
else
{
49
//: %1 will be replaced by the field label
50
//% "The “%1” field is required."
51
return
c->
qtTrId
(
"cutelyst-validator-genvalerr-req-label"
).
arg
(_label);
52
}
53
}
Cutelyst::Context
The Cutelyst Context.
Definition
context.h:42
Cutelyst::Context::qtTrId
QString qtTrId(const char *id, int n=-1) const
Definition
context.h:656
Cutelyst::ValidatorRequired::~ValidatorRequired
~ValidatorRequired() override
Cutelyst::ValidatorRequired::validate
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Definition
validatorrequired.cpp:18
Cutelyst::ValidatorRequired::genericValidationError
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
Definition
validatorrequired.cpp:40
Cutelyst::ValidatorRequired::ValidatorRequired
ValidatorRequired(const QString &field, const ValidatorMessages &messages=ValidatorMessages())
Definition
validatorrequired.cpp:10
Cutelyst::ValidatorRule
Base class for all validator rules.
Definition
validatorrule.h:304
Cutelyst::ValidatorRule::field
QString field() const noexcept
Definition
validatorrule.cpp:28
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::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::contains
bool contains(const Key &key) 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
validatorrequired.cpp
Generated by
1.9.8