6 #include "validatorrule_p.h" 7 #include <Cutelyst/Context> 8 #include <Cutelyst/ParamsMultiMap> 13 d_ptr(new ValidatorRulePrivate(field, messages, defValKey))
34 if (!d->field.isEmpty() && !params.
empty()) {
36 v = params.
value(d->field).trimmed();
38 v = params.
value(d->field);
49 if (d->messages.label) {
50 if (d->translationContext.size()) {
51 l = c->
translate(d->translationContext.data(), d->messages.label);
64 if (d->messages.validationError) {
65 if (d->translationContext.size()) {
66 error = c->
translate(d->translationContext.data(), d->messages.validationError);
82 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data in the “%1” field is not acceptable.").
arg(_label);
84 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data is not acceptable.");
94 if (d->messages.parsingError) {
95 if (d->translationContext.size()) {
96 error = c->
translate(d->translationContext.data(), d->messages.parsingError);
112 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data in the “%1“ field could not be parsed.").
arg(_label);
114 error = c->
translate(
"Cutelyst::ValidatorRule",
"The input data could not be parsed.");
124 if (d->messages.validationDataError) {
125 if (d->translationContext.size()) {
126 error = c->
translate(d->translationContext.data(), d->messages.validationDataError);
142 error = c->
translate(
"Cutelyst::ValidatorRule",
"Missing or invalid validation data for the “%1” field.").
arg(_label);
144 error = c->
translate(
"Cutelyst::ValidatorRule",
"Missing or invalid validation data.");
151 Q_ASSERT_X(c,
"getting default value",
"invalid context object");
152 Q_ASSERT_X(result,
"getting default value",
"invalid result object");
153 Q_ASSERT_X(validatorName,
"getting default value",
"invalid validator name");
155 if (!d->defValKey.isEmpty() && c->
stash().contains(d->defValKey)) {
157 qCDebug(C_VALIDATOR,
"%s: Using default value \"%s\" for field %s in %s::%s.",
161 qPrintable(c->controllerName()),
162 qPrintable(c->actionName()));
168 void ValidatorRule::setTrimBefore(
bool trimBefore)
174 void ValidatorRule::setTranslationContext(
QLatin1String trContext)
177 d->translationContext = trContext;
ValidatorRule(const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new ValidatorRule with the given parameters.
virtual QString genericParsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if an error occures while parsing input.
QString parsingError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if an error occured while parsing input.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Stores custom error messages and the input field label.
virtual QString genericValidationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns a generic error message if any validation data is missing or invalid.
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)
void stash(const QVariantHash &unite)
bool isEmpty() const const
bool trimBefore() const
Returns true if the field value should be trimmed before validation.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
The Cutelyst namespace holds all public Cutelyst API.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void setValue(const T &value)
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
virtual ~ValidatorRule()
Deconstructs the ValidatorRule.
QString field() const
Returns the name of the field to validate.
Contains the result of a single input parameter validation.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString toString() const const
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 ...
const T value(const Key &key, const T &defaultValue) const const