cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatoralpha.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatoralpha_p.h"
7
8using namespace Cutelyst;
9
10const QRegularExpression ValidatorAlphaPrivate::regex{u"^[\\pL\\pM]+$"_qs};
11
12ValidatorAlpha::ValidatorAlpha(const QString &field,
13 bool asciiOnly,
14 const Cutelyst::ValidatorMessages &messages,
15 const QString &defValKey)
16 : ValidatorRule(*new ValidatorAlphaPrivate(field, asciiOnly, messages, defValKey))
17{
18}
19
21
23 const ParamsMultiMap &params) const
24{
26
27 Q_D(const ValidatorAlpha);
28
29 const QString v = value(params);
30 if (!v.isEmpty()) {
31 if (Q_LIKELY(ValidatorAlpha::validate(v, d->asciiOnly))) {
32 result.value.setValue(v);
33 } else {
34 qCDebug(C_VALIDATOR).noquote().nospace()
35 << debugString(c) << " \"" << v << "\" contains character that are not allowed";
36 result.errorMessage = validationError(c);
37 }
38 } else {
39 defaultValue(c, &result);
40 }
41
42 return result;
43}
44
45bool ValidatorAlpha::validate(const QString &value, bool asciiOnly)
46{
47 bool valid = true;
48
49 if (asciiOnly) {
50 for (const QChar &ch : value) {
51 const ushort &uc = ch.unicode();
52 if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
53 (uc <= ValidatorRulePrivate::ascii_Z)) ||
54 ((uc >= ValidatorRulePrivate::ascii_a) &&
55 (uc <= ValidatorRulePrivate::ascii_z)))) {
56 valid = false;
57 break;
58 }
59 }
60 } else {
61 valid = value.contains(ValidatorAlphaPrivate::regex);
62 }
63
64 return valid;
65}
66
67QString ValidatorAlpha::genericValidationError(Context *c, const QVariant &errorData) const
68{
69 QString error;
70 Q_UNUSED(errorData)
71 Q_D(const ValidatorAlpha);
72 const QString _label = label(c);
73 if (_label.isEmpty()) {
74 if (d->asciiOnly) {
75 error = c->translate("Cutelyst::ValidatorAlhpa",
76 "Must only contain alphabetical latin characters.");
77 } else {
78 error = c->translate("Cutelyst::ValidatorAlhpa",
79 "Must only contain alphabetical characters.");
80 }
81 } else {
82 if (d->asciiOnly) {
83 //: %1 will be replaced by the field label
84 error =
85 c->translate(
86 "Cutelyst::ValidatorAlhpa",
87 "The text in the “%1” field must only contain alphabetical latin characters.")
88 .arg(_label);
89 } else {
90 //: %1 will be replaced by the field label
91 error = c->translate(
92 "Cutelyst::ValidatorAlhpa",
93 "The text in the “%1” field must only contain alphabetical characters.")
94 .arg(_label);
95 }
96 }
97 return error;
98}
The Cutelyst Context.
Definition: context.h:38
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Validates an input field for only alphabetic content.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
~ValidatorAlpha() override
Deconstructs the alpha validator.
ValidatorAlpha(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new alpha validator.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void defaultValue(Context *c, ValidatorReturnType *result) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
static bool validate(const QString &value, bool asciiOnly=false)
Returns true if value only contains alphabetic characters.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49