cutelyst 4.3.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-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatoralphanum_p.h"
7
8using namespace Cutelyst;
9
10const QRegularExpression ValidatorAlphaNumPrivate::regex{u"^[\\pL\\pM\\pN]+$"_qs};
11
13 bool asciiOnly,
14 const ValidatorMessages &messages,
15 const QString &defValKey)
16 : ValidatorRule(*new ValidatorAlphaNumPrivate(field, asciiOnly, messages, defValKey))
17{
18}
19
21
23{
25
26 Q_D(const ValidatorAlphaNum);
27
28 const QString v = value(params);
29 if (!v.isEmpty()) {
30 if (Q_LIKELY(ValidatorAlphaNum::validate(v, d->asciiOnly))) {
31 result.value.setValue(v);
32 } else {
33 qCDebug(C_VALIDATOR).noquote().nospace()
34 << debugString(c) << " \"" << v << "\" contains character that are not allowed";
35 result.errorMessage = validationError(c);
36 }
37
38 } else {
39 defaultValue(c, &result);
40 }
41
42 return result;
43}
44
45bool ValidatorAlphaNum::validate(const QString &value, bool asciiOnly)
46{
47 bool valid = true;
48 if (asciiOnly) {
49 for (const QChar &ch : value) {
50 const ushort &uc = ch.unicode();
51 if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
52 (uc <= ValidatorRulePrivate::ascii_Z)) ||
53 ((uc >= ValidatorRulePrivate::ascii_a) &&
54 (uc <= ValidatorRulePrivate::ascii_z)) ||
55 ((uc >= ValidatorRulePrivate::ascii_0) &&
56 (uc <= ValidatorRulePrivate::ascii_9)))) {
57 valid = false;
58 break;
59 }
60 }
61 } else {
62 valid = value.contains(ValidatorAlphaNumPrivate::regex);
63 }
64 return valid;
65}
66
68{
69 QString error;
70 Q_UNUSED(errorData)
71 Q_D(const ValidatorAlphaNum);
72 const QString _label = label(c);
73 if (_label.isEmpty()) {
74 if (d->asciiOnly) {
75 //% "Must only contain alpha-numeric latin characters from the ASCII "
76 //% "character encondig (a-z, A-Z and 0-9)."
77 return c->qtTrId("cutelyst-valalphanum-genvalerr-asciionly");
78 } else {
79 //% "Must only contain alpha-numeric characters."
80 return c->qtTrId("cutelyst-valalphanum-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 alpha-numeric latin characters "
86 //% "from the ASCII character encondig (a-z, A-Z and 0-9)."
87 return c->qtTrId("cutelyst-valalphanum-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 alpha-numeric characters."
91 return c->qtTrId("cutelyst-valalphanum-genvalerr-label").arg(_label);
92 }
93 }
94 return error;
95}
The Cutelyst Context.
Definition context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition context.h:656
Checks a value for only alpha-numeric content.
ValidatorAlphaNum(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
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 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.