cutelyst 5.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-2025 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatoralpha_p.h"
7
8using namespace Cutelyst;
9using namespace Qt::Literals::StringLiterals;
10
11const QRegularExpression ValidatorAlphaPrivate::regex{u"^[\\pL\\pM]+$"_s};
12
14 bool asciiOnly,
15 const Cutelyst::ValidatorMessages &messages,
16 const QString &defValKey)
17 : ValidatorRule(*new ValidatorAlphaPrivate(field, asciiOnly, messages, defValKey))
18{
19}
20
22
24 const ParamsMultiMap &params) const
25{
27
28 Q_D(const ValidatorAlpha);
29
30 const QString v = value(params);
31 if (!v.isEmpty()) {
32 if (Q_LIKELY(ValidatorAlpha::validate(v, d->asciiOnly))) {
33 result.value.setValue(v);
34 } else {
35 qCDebug(C_VALIDATOR).noquote().nospace()
36 << debugString(c) << " \"" << v << "\" contains character that are not allowed";
37 result.errorMessage = validationError(c);
38 }
39 } else {
40 defaultValue(c, &result);
41 }
42
43 return result;
44}
45
47{
48 cb(validate(c, params));
49}
50
51bool ValidatorAlpha::validate(const QString &value, bool asciiOnly)
52{
53 bool valid = true;
54
55 if (asciiOnly) {
56 for (const QChar &ch : value) {
57 const ushort &uc = ch.unicode();
58 if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
59 (uc <= ValidatorRulePrivate::ascii_Z)) ||
60 ((uc >= ValidatorRulePrivate::ascii_a) &&
61 (uc <= ValidatorRulePrivate::ascii_z)))) {
62 valid = false;
63 break;
64 }
65 }
66 } else {
67 valid = value.contains(ValidatorAlphaPrivate::regex);
68 }
69
70 return valid;
71}
72
74{
75 Q_UNUSED(errorData)
76 Q_D(const ValidatorAlpha);
77 const QString _label = label(c);
78 if (_label.isEmpty()) {
79 if (d->asciiOnly) {
80 //% "Must only contain alphabetical characters from the ASCII character "
81 //% "encoding (a-z and A-Z)."
82 return c->qtTrId("cutelyst-valalpha-genvalerr-asciionly");
83 } else {
84 //% "Must only contain alphabetical characters."
85 return c->qtTrId("cutelyst-valalpha-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 alphabetical characters "
91 //% "from the ASCII character encoding (a-z and A-Z)."
92 return c->qtTrId("cutelyst-valalpha-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 alphabetical characters."
96 return c->qtTrId("cutelyst-valalpha-genvalerr-label").arg(_label);
97 }
98 }
99}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:657
Validates an input field for only alphabetic content.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorAlpha(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages={}, const QString &defValKey={})
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
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 alphabetic 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.