cutelyst 4.3.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
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
68{
69 Q_UNUSED(errorData)
70 Q_D(const ValidatorAlpha);
71 const QString _label = label(c);
72 if (_label.isEmpty()) {
73 if (d->asciiOnly) {
74 //% "Must only contain alphabetical characters from the ASCII character "
75 //% "encoding (a-z and A-Z)."
76 return c->qtTrId("cutelyst-valalpha-genvalerr-asciionly");
77 } else {
78 //% "Must only contain alphabetical characters."
79 return c->qtTrId("cutelyst-valalpha-genvalerr");
80 }
81 } else {
82 if (d->asciiOnly) {
83 //: %1 will be replaced by the field label
84 //% "The text in the “%1” field must only contain alphabetical characters "
85 //% "from the ASCII character encoding (a-z and A-Z)."
86 return c->qtTrId("cutelyst-valalpha-genvalerr-asciionly-label").arg(_label);
87 } else {
88 //: %1 will be replaced by the field label
89 //% "The text in the “%1” field must only contain alphabetical characters."
90 return c->qtTrId("cutelyst-valalpha-genvalerr-label").arg(_label);
91 }
92 }
93}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
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=ValidatorMessages(), const QString &defValKey=QString())
Base class for all validator rules.
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
void defaultValue(Context *c, ValidatorReturnType *result) const
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) 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.