cutelyst 4.8.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatoralphadash.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "validatoralphadash_p.h"
7
8using namespace Cutelyst;
9using namespace Qt::Literals::StringLiterals;
10
11const QRegularExpression ValidatorAlphaDashPrivate::regex{u"^[\\pL\\pM\\pN_-]+$"_s};
12
14 bool asciiOnly,
15 const ValidatorMessages &messages,
16 const QString &defValKey)
17 : ValidatorRule(*new ValidatorAlphaDashPrivate(field, asciiOnly, messages, defValKey))
18{
19}
20
22
24{
26
27 Q_D(const ValidatorAlphaDash);
28
29 const QString v = value(params);
30 if (!v.isEmpty()) {
31 if (Q_LIKELY(ValidatorAlphaDash::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 ValidatorAlphaDash::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 (uc == ValidatorRulePrivate::ascii_dash) ||
58 (uc == ValidatorRulePrivate::ascii_underscore))) {
59 valid = false;
60 break;
61 }
62 }
63 } else {
64 valid = value.contains(ValidatorAlphaDashPrivate::regex);
65 }
66 return valid;
67}
68
70 const QVariant &errorData) const
71{
72 Q_UNUSED(errorData)
73 Q_D(const ValidatorAlphaDash);
74 const QString _label = label(c);
75 if (_label.isEmpty()) {
76 if (d->asciiOnly) {
77 //% "Must only contain alpha-numeric latin characters, dashes and underscores "
78 //% "from the ASCII character encoding (a-z, A-Z, 0-9, _ and -)."
79 return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly");
80 } else {
81 //% "Must only contain alpha-numeric characters, dashes and underscores."
82 return c->qtTrId("cutelyst-valalphadash-genvalerr");
83 }
84 } else {
85 if (d->asciiOnly) {
86 //: %1 will be replaced by the field label
87 //% "The text in the “%1” field must only contain alpha-numeric latin "
88 //% "characters, dashes and underscores from the ASCII character encondig "
89 //% "(a-z, A-Z, 0-9, _ and -)."
90 return c->qtTrId("cutelyst-valalphadash-genvalerr-asciionly-label").arg(_label);
91 } else {
92 //: %1 will be replaced by the field label
93 //% "The text in the “%1” field must only contain alpha-numeric "
94 //% "characters, dashes and underscores."
95 return c->qtTrId("cutelyst-valalphadash-genvalerr-label").arg(_label);
96 }
97 }
98}
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
ValidatorAlphaDash(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 the value only contains alpha-numeric characters, dashes and underscores.
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.