cutelyst 4.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-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
67QString ValidatorAlphaNum::genericValidationError(Context *c, const QVariant &errorData) const
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 error = c->translate("Cutelyst::ValidatorAlphaNum",
76 "Must only contain alpha-numeric latin characters.");
77 } else {
78 error = c->translate("Cutelyst::ValidatorAlphaNum",
79 "Must only contain alpha-numeric characters.");
80 }
81 } else {
82 if (d->asciiOnly) {
83 //: %1 will be replaced by the field label
84 error =
85 c->translate(
86 "Cutelyst::ValidatorAlphaNum",
87 "The text in the “%1” field must only contain alpha-numeric latin characters.")
88 .arg(_label);
89 } else {
90 //: %1 will be replaced by the field label
91 error = c->translate(
92 "Cutelyst::ValidatorAlphaNum",
93 "The text in the “%1” field must only contain alpha-numeric characters.")
94 .arg(_label);
95 }
96 }
97 return error;
98}
The Cutelyst Context.
Definition: context.h:38
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:477
Checks a value for only alpha-numeric content.
ValidatorAlphaNum(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new alpha num validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
~ValidatorAlphaNum() override
Deconstructs the alpha num validator.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void defaultValue(Context *c, ValidatorReturnType *result) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
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.
Definition: Mainpage.dox:8
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49