19 #include "validatorrule_p.h"
20 #include <Cutelyst/Context>
21 #include <Cutelyst/ParamsMultiMap>
23 using namespace Cutelyst;
26 d_ptr(new ValidatorRulePrivate(field, messages, defValKey))
47 if (!d->field.isEmpty() && !params.
empty()) {
49 v = params.
value(d->field).trimmed();
51 v = params.
value(d->field);
62 if (d->messages.label) {
63 if (d->translationContext.size()) {
64 l = c->
translate(d->translationContext.data(), d->messages.label);
77 if (d->messages.validationError) {
78 if (d->translationContext.size()) {
79 error = c->
translate(d->translationContext.data(), d->messages.validationError);
95 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data in the “%1” field is not acceptable.").
arg(_label);
97 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data is not acceptable.");
107 if (d->messages.parsingError) {
108 if (d->translationContext.size()) {
109 error = c->
translate(d->translationContext.data(), d->messages.parsingError);
125 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data in the “%1“ field could not be parsed.").
arg(_label);
127 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data could not be parsed.");
137 if (d->messages.validationDataError) {
138 if (d->translationContext.size()) {
139 error = c->
translate(d->translationContext.data(), d->messages.validationDataError);
155 error = c->
translate(
"Cutelyst::ValidatorRule",
"Missing or invalid validation data for the “%1” field.").
arg(_label);
157 error = c->
translate(
"Cutelyst::ValidatorRule",
"Missing or invalid validation data.");
164 Q_ASSERT_X(c,
"getting default value",
"invalid context object");
165 Q_ASSERT_X(result,
"getting default value",
"invalid result object");
166 Q_ASSERT_X(validatorName,
"getting default value",
"invalid validator name");
168 if (!d->defValKey.isEmpty() && c->
stash().contains(d->defValKey)) {
170 qCDebug(C_VALIDATOR,
"%s: Using default value \"%s\" for field %s in %s::%s.",
174 qPrintable(c->controllerName()),
175 qPrintable(c->actionName()));
181 void ValidatorRule::setTrimBefore(
bool trimBefore)
187 void ValidatorRule::setTranslationContext(
QLatin1String trContext)
190 d->translationContext = trContext;
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
bool trimBefore() const
Returns true if the field value should be trimmed before validation.
QString field() const
Returns the name of the field to validate.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
Stores custom error messages and the input field label.
virtual QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error mesage if validation failed.
QString fromUtf8(const char *str, int size)
QString value(const ParamsMultiMap ¶ms) 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.
virtual QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if any validation data is missing or invalid.
Base class for all validator rules.
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 ...
void setValue(const T &value)
virtual QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if an error occures while parsing input.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Contains the result of a single input parameter validation.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
void stash(const QVariantHash &unite)
const T value(const Key &key, const T &defaultValue) const
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.