cutelyst 5.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatoralphanum.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2025 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatoralphanum_p.h"
7
8using namespace Cutelyst;
9using namespace Qt::Literals::StringLiterals;
10
11const QRegularExpression ValidatorAlphaNumPrivate::regex{u"^[\\pL\\pM\\pN]+$"_s};
12
14 bool asciiOnly,
15 const ValidatorMessages &messages,
16 const QString &defValKey)
17 : ValidatorRule(*new ValidatorAlphaNumPrivate(field, asciiOnly, messages, defValKey))
18{
19}
20
22
24{
26
27 Q_D(const ValidatorAlphaNum);
28
29 const QString v = value(params);
30 if (!v.isEmpty()) {
31 if (Q_LIKELY(ValidatorAlphaNum::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
39 } else {
40 defaultValue(c, &result);
41 }
42
43 return result;
44}
45
47{
48 cb(validate(c, params));
49}
50
51bool ValidatorAlphaNum::validate(const QString &value, bool asciiOnly)
52{
53 bool valid = true;
54 if (asciiOnly) {
55 for (const QChar &ch : value) {
56 const ushort &uc = ch.unicode();
57 if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
58 (uc <= ValidatorRulePrivate::ascii_Z)) ||
59 ((uc >= ValidatorRulePrivate::ascii_a) &&
60 (uc <= ValidatorRulePrivate::ascii_z)) ||
61 ((uc >= ValidatorRulePrivate::ascii_0) &&
62 (uc <= ValidatorRulePrivate::ascii_9)))) {
63 valid = false;
64 break;
65 }
66 }
67 } else {
68 valid = value.contains(ValidatorAlphaNumPrivate::regex);
69 }
70 return valid;
71}
72
74{
75 Q_UNUSED(errorData)
76 Q_D(const ValidatorAlphaNum);
77 const QString _label = label(c);
78 if (_label.isEmpty()) {
79 if (d->asciiOnly) {
80 //% "Must only contain alpha-numeric latin characters from the ASCII "
81 //% "character encondig (a-z, A-Z and 0-9)."
82 return c->qtTrId("cutelyst-valalphanum-genvalerr-asciionly");
83 } else {
84 //% "Must only contain alpha-numeric characters."
85 return c->qtTrId("cutelyst-valalphanum-genvalerr");
86 }
87 } else {
88 if (d->asciiOnly) {
89 //: %1 will be replaced by the field label
90 //% "The text in the “%1” field must only contain alpha-numeric latin characters "
91 //% "from the ASCII character encondig (a-z, A-Z and 0-9)."
92 return c->qtTrId("cutelyst-valalphanum-genvalerr-asciionly-label").arg(_label);
93 } else {
94 //: %1 will be replaced by the field label
95 //% "The text in the “%1” field must only contain alpha-numeric characters."
96 return c->qtTrId("cutelyst-valalphanum-genvalerr-label").arg(_label);
97 }
98 }
99 return {};
100}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
Checks a value for only alpha-numeric content.
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorAlphaNum(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages={}, const QString &defValKey={})
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(const Context *c) const
QString debugString(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
static bool validate(const QString &value, bool asciiOnly=false)
Returns true if value only contains alpha-numeric characters.
The Cutelyst namespace holds all public Cutelyst API.
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) 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.