cutelyst 4.0.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;
9
10const QRegularExpression ValidatorAlphaDashPrivate::regex{u"^[\\pL\\pM\\pN_-]+$"_qs};
11
13 bool asciiOnly,
14 const ValidatorMessages &messages,
15 const QString &defValKey)
16 : ValidatorRule(*new ValidatorAlphaDashPrivate(field, asciiOnly, messages, defValKey))
17{
18}
19
21
23{
25
26 Q_D(const ValidatorAlphaDash);
27
28 const QString v = value(params);
29 if (!v.isEmpty()) {
30 if (Q_LIKELY(ValidatorAlphaDash::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 } else {
38 defaultValue(c, &result);
39 }
40
41 return result;
42}
43
44bool ValidatorAlphaDash::validate(const QString &value, bool asciiOnly)
45{
46 bool valid = true;
47 if (asciiOnly) {
48 for (const QChar &ch : value) {
49 const ushort &uc = ch.unicode();
50 if (!(((uc >= ValidatorRulePrivate::ascii_A) &&
51 (uc <= ValidatorRulePrivate::ascii_Z)) ||
52 ((uc >= ValidatorRulePrivate::ascii_a) &&
53 (uc <= ValidatorRulePrivate::ascii_z)) ||
54 ((uc >= ValidatorRulePrivate::ascii_0) &&
55 (uc <= ValidatorRulePrivate::ascii_9)) ||
56 (uc == ValidatorRulePrivate::ascii_dash) ||
57 (uc == ValidatorRulePrivate::ascii_underscore))) {
58 valid = false;
59 break;
60 }
61 }
62 } else {
63 valid = value.contains(ValidatorAlphaDashPrivate::regex);
64 }
65 return valid;
66}
67
69 const QVariant &errorData) const
70{
71 QString error;
72 Q_UNUSED(errorData)
73 Q_D(const ValidatorAlphaDash);
74 const QString _label = label(c);
75 if (_label.isEmpty()) {
76 if (d->asciiOnly) {
77 error = c->translate(
78 "Cutelyst::ValidatorAlphaDash",
79 "Must only contain alpha-numeric latin characters, dashes and underscores.");
80 } else {
81 error =
82 c->translate("Cutelyst::ValidatorAlphaDash",
83 "Must only contain alpha-numeric characters, dashes and underscores.");
84 }
85 } else {
86 if (d->asciiOnly) {
87 //: %1 will be replaced by the field label
88 error = c->translate("Cutelyst::ValidatorAlphaDash",
89 "The text in the “%1” field must only contain alpha-numeric latin "
90 "characters, dashes and underscores.")
91 .arg(_label);
92 } else {
93 //: %1 will be replaced by the field label
94 error = c->translate("Cutelyst::ValidatorAlphaDash",
95 "The text in the “%1” field must only contain alpha-numeric "
96 "characters, dashes and underscores.")
97 .arg(_label);
98 }
99 }
100 return error;
101}
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 and dashes and underscores.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message.
ValidatorAlphaDash(const QString &field, bool asciiOnly=false, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new alpha dash validator.
~ValidatorAlphaDash() override
Deconstructs the alpha dash 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 the value only contains alpha-numeric characters, dashes and underscores.
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