6#include "validatorrule_p.h"
8#include <Cutelyst/Context>
9#include <Cutelyst/ParamsMultiMap>
16 : d_ptr(new ValidatorRulePrivate(
field, messages, defValKey))
41 if (!d->field.isEmpty() && !params.empty()) {
43 v = params.value(d->field).trimmed();
45 v = params.value(d->field);
56 if (d->messages.label) {
57 if (d->translationContext.size()) {
58 l = c->
translate(d->translationContext.data(), d->messages.label);
71 if (d->messages.validationError) {
72 if (d->translationContext.size()) {
73 error = c->
translate(d->translationContext.data(), d->messages.validationError);
89 error = c->
translate(
"Cutelyst::ValidatorRule",
90 "The input data in the “%1” field is not acceptable.")
93 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data is not acceptable.");
103 if (d->messages.parsingError) {
104 if (d->translationContext.size()) {
105 error = c->
translate(d->translationContext.data(), d->messages.parsingError);
121 error = c->
translate(
"Cutelyst::ValidatorRule",
122 "The input data in the “%1“ field could not be parsed.")
125 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data could not be parsed.");
135 if (d->messages.validationDataError) {
136 if (d->translationContext.size()) {
137 error = c->
translate(d->translationContext.data(), d->messages.validationDataError);
153 error = c->
translate(
"Cutelyst::ValidatorRule",
154 "Missing or invalid validation data for the “%1” field.")
157 error = c->
translate(
"Cutelyst::ValidatorRule",
"Missing or invalid validation data.");
164 const char *validatorName)
const
166 Q_ASSERT_X(c,
"getting default value",
"invalid context object");
167 Q_ASSERT_X(result,
"getting default value",
"invalid result object");
168 Q_ASSERT_X(validatorName,
"getting default value",
"invalid validator name");
170 if (!d->defValKey.isEmpty() && c->
stash().contains(d->defValKey)) {
173 "%s: Using default value \"%s\" for field %s in %s::%s.",
177 qPrintable(c->controllerName()),
178 qPrintable(c->actionName()));
185 return d->trimBefore;
188void ValidatorRule::setTrimBefore(
bool trimBefore)
194void ValidatorRule::setTranslationContext(
QLatin1String trContext)
197 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 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.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
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 ...
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
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.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString fromUtf8(const char *str, int size)
bool isEmpty() const const
void setValue(const T &value)
QString toString() const const
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.