cutelyst 4.8.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;
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
46bool ValidatorAlpha::validate(const QString &value, bool asciiOnly)
47{
48 bool valid = true;
49
50 if (asciiOnly) {
51 for (const QChar &ch : value) {
52 const ushort &uc = ch.unicode();
53 if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
54 (uc <= ValidatorRulePrivate::ascii_Z)) ||
55 ((uc >= ValidatorRulePrivate::ascii_a) &&
56 (uc <= ValidatorRulePrivate::ascii_z)))) {
57 valid = false;
58 break;
59 }
60 }
61 } else {
62 valid = value.contains(ValidatorAlphaPrivate::regex);
63 }
64
65 return valid;
66}
67
69{
70 Q_UNUSED(errorData)
71 Q_D(const ValidatorAlpha);
72 const QString _label = label(c);
73 if (_label.isEmpty()) {
74 if (d->asciiOnly) {
75 //% "Must only contain alphabetical characters from the ASCII character "
76 //% "encoding (a-z and A-Z)."
77 return c->qtTrId("cutelyst-valalpha-genvalerr-asciionly");
78 } else {
79 //% "Must only contain alphabetical characters."
80 return c->qtTrId("cutelyst-valalpha-genvalerr");
81 }
82 } else {
83 if (d->asciiOnly) {
84 //: %1 will be replaced by the field label
85 //% "The text in the “%1” field must only contain alphabetical characters "
86 //% "from the ASCII character encoding (a-z and A-Z)."
87 return c->qtTrId("cutelyst-valalpha-genvalerr-asciionly-label").arg(_label);
88 } else {
89 //: %1 will be replaced by the field label
90 //% "The text in the “%1” field must only contain alphabetical characters."
91 return c->qtTrId("cutelyst-valalpha-genvalerr-label").arg(_label);
92 }
93 }
94}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorAlpha(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString field() const noexcept
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
ValidatorRule(const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr)
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
QMultiMap< QString, QString > ParamsMultiMap
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 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.