6#include "validatorrule_p.h"
8#include <Cutelyst/Context>
9#include <Cutelyst/ParamsMultiMap>
15 const QString &defValKey,
16 QByteArrayView validatorName)
17 : d_ptr(new ValidatorRulePrivate(field, messages, defValKey, validatorName))
38 if (!d->field.isEmpty() && !params.empty()) {
40 return params.value(d->field).trimmed();
42 return params.value(d->field);
53 if (d->messages.label) {
54 if (d->translationContext.size()) {
55 l = c->
translate(d->translationContext.data(), d->messages.label);
57 l = QString::fromUtf8(d->messages.label);
68 if (d->messages.validationError) {
69 if (d->translationContext.size()) {
70 error = c->
translate(d->translationContext.data(), d->messages.validationError);
72 error = QString::fromUtf8(d->messages.validationError);
84 const QString _label =
label(c);
85 if (!_label.isEmpty()) {
86 error = c->
translate(
"Cutelyst::ValidatorRule",
87 "The input data in the “%1” field is not acceptable.")
90 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data is not acceptable.");
100 if (d->messages.parsingError) {
101 if (d->translationContext.size()) {
102 error = c->
translate(d->translationContext.data(), d->messages.parsingError);
104 error = QString::fromUtf8(d->messages.parsingError);
116 const QString _label =
label(c);
117 if (!_label.isEmpty()) {
118 error = c->
translate(
"Cutelyst::ValidatorRule",
119 "The input data in the “%1“ field could not be parsed.")
122 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data could not be parsed.");
132 if (d->messages.validationDataError) {
133 if (d->translationContext.size()) {
134 error = c->
translate(d->translationContext.data(), d->messages.validationDataError);
136 error = QString::fromUtf8(d->messages.validationDataError);
148 const QString _label =
label(c);
149 if (!_label.isEmpty()) {
150 error = c->
translate(
"Cutelyst::ValidatorRule",
151 "Missing or invalid validation data for the “%1” field.")
154 error = c->
translate(
"Cutelyst::ValidatorRule",
"Missing or invalid validation data.");
161 Q_ASSERT_X(c,
"getting default value",
"invalid context object");
162 Q_ASSERT_X(result,
"getting default value",
"invalid result object");
164 if (!d->defValKey.isEmpty() && c->
stash().contains(d->defValKey)) {
165 result->
value.setValue(c->
stash(d->defValKey));
166 qCDebug(C_VALIDATOR).noquote().nospace()
167 << d->validatorName <<
": Using default value " << result->
value <<
" for field \""
168 <<
field() <<
"\" at " << c->controllerName() <<
"::" << c->actionName();
175 return QString::fromLatin1(d->validatorName) +
176 QLatin1String(
": Validation failed for field \"") +
field() + QLatin1String(
"\" at ") +
177 c->controllerName() + QLatin1String(
"::") + c->actionName() + QLatin1Char(
':');
183 return d->trimBefore;
186void ValidatorRule::setTrimBefore(
bool trimBefore)
noexcept
189 d->trimBefore = trimBefore;
192void ValidatorRule::setTranslationContext(QLatin1String trContext)
noexcept
195 d->translationContext = trContext;
void stash(const QVariantHash &unite)
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Base class for all validator rules.
virtual QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error mesage if validation failed.
QString field() const noexcept
Returns the name of the field to validate.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString(), QByteArrayView validatorName=nullptr)
Constructs a new ValidatorRule with the given parameters.
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.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
bool trimBefore() const noexcept
Returns true if the field value should be trimmed before validation.
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 ¶ms) 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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.