cutelyst 3.9.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorrule.h
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2022 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#ifndef CUTELYSTVALIDATORRULE_H
6#define CUTELYSTVALIDATORRULE_H
7
8#include <Cutelyst/cutelyst_global.h>
9#include <Cutelyst/paramsmultimap.h>
10
11#include <QLoggingCategory>
12#include <QScopedPointer>
13#include <QVariant>
14
15Q_DECLARE_LOGGING_CATEGORY(C_VALIDATOR)
16
17namespace Cutelyst {
18
37
38class Context;
39
49struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorReturnType {
61
67 explicit operator bool() const { return errorMessage.isNull(); }
68
74 bool isValid() const { return errorMessage.isNull(); }
75};
76
135 * ValidatorMessages("Required Field", "This field is
136 * required, please enter data."))
137 * });
138 * }
139 * \endcode
140 */
141struct CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorMessages {
150 * as used on the frontend visible to the user. Will be used by generic error messages if set.
151 * \param customValidationError Custom error message if the validation fails.
152 * \param customParsingError Custom error message if the input value could not be parsed.
153 * \param customValidationDataError Custom error message if validation data is missing or
154 * invalid.
155 */
156 ValidatorMessages(const char *customLabel,
157 const char *customValidationError = nullptr,
158 const char *customParsingError = nullptr,
159 const char *customValidationDataError = nullptr)
160 : label(customLabel)
161 , validationError(customValidationError)
162 , parsingError(customParsingError)
163 , validationDataError(customValidationDataError)
164 {
165 }
166 const char *label = nullptr;
167 const char *validationError = nullptr;
168 const char *parsingError = nullptr;
169 const char *validationDataError = nullptr;
170};
171
172class ValidatorRulePrivate;
173
292 * %2").arg(_label, m_compareValue);
293 * }
294 * return error;
295 * }
296 * \endcode
297 *
298 * That's it. Now you can use your own validator rule in the main Validator.
299 */
300class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorRule
301{
302public:
311 const ValidatorMessages &messages = ValidatorMessages(),
312 const QString &defValKey = QString());
313
317 virtual ~ValidatorRule();
318
319protected:
325 ValidatorRule(ValidatorRulePrivate &dd);
326
371 virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const = 0;
372
377 QString field() const;
378
384 QString label(Context *c) const;
385
389 QString value(const ParamsMultiMap &params) const;
390
397 bool trimBefore() const;
398
413 QString validationError(Context *c, const QVariant &errorData = QVariant()) const;
414
446 const QVariant &errorData = QVariant()) const;
447
462 QString parsingError(Context *c, const QVariant &errorData = QVariant()) const;
463
494 virtual QString genericParsingError(Context *c, const QVariant &errorData = QVariant()) const;
495
510 QString validationDataError(Context *c, const QVariant &errorData = QVariant()) const;
511
543 const QVariant &errorData = QVariant()) const;
544
551 void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const;
552
553private:
554 Q_DECLARE_PRIVATE(ValidatorRule)
555 Q_DISABLE_COPY(ValidatorRule)
556
557
562 void setTranslationContext(QLatin1String trContext);
563
571
574 void setTrimBefore(bool trimBefore);
575
576 friend class Validator;
577 friend class ValidatorPrivate;
578};
579
580} // namespace Cutelyst
581
582#endif // CUTELYSTVALIDATORRULE_H
The Cutelyst Context.
Definition context.h:39
virtual QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error mesage if validation failed.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString field() const
Returns the name of the field to validate.
bool trimBefore() const
Returns true if the field value should be trimmed before validation.
virtual QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if any validation data is missing or invalid.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occurred while parsing input.
void defaultValue(Context *c, ValidatorReturnType *result, const char *validatorName) const
I a defValKey has been set in the constructor, this will try to get the default value from the stash ...
virtual ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const =0
Starts the validation and returns the result.
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
virtual QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if an error occures while parsing input.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
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.
ValidatorMessages()
Constructs a default ValidatorMessages object with all custom messages disabled.
Contains the result of a single input parameter validation.
bool isValid() const
Returns true if validation succeeded.