19 #include "validatorin_p.h"
24 ValidatorRule(*new ValidatorInPrivate(field, values, cs, messages, defValKey))
38 const QString v =
value(params);
42 if (d->values.type() == QVariant::StringList) {
43 vals = d->values.toStringList();
44 }
else if (d->values.type() == QVariant::String) {
45 vals = c->
stash(d->values.toString()).toStringList();
49 qCWarning(C_VALIDATOR,
"ValidatorIn: The list of comparison values for the field %s at %s::%s is empty.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
52 if (vals.contains(v, d->cs)) {
53 result.
value.setValue<QString>(v);
55 qCDebug(C_VALIDATOR,
"ValidatorIn: Validation failed for field %s at %s::%s: \"%s\" is not part of the list of comparison values.", qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), qPrintable(v));
69 const QStringList vals = errorData.toStringList();
70 const QString _label =
label(c);
71 if (_label.isEmpty()) {
73 error = c->
translate(
"Cutelyst::ValidatorIn",
"Has to be one of the following values: %1").arg(c->
locale().createSeparatedList(vals));
76 error = c->
translate(
"Cutelyst::ValidatorIn",
"The value in the “%1” field has to be one of the following values: %2").arg(_label, c->
locale().createSeparatedList(vals));
85 const QString _label =
label(c);
86 if (_label.isEmpty()) {
87 error = c->
translate(
"Cutelyst::ValidatorIn",
"The list of comparison values is empty.");
90 error = c->
translate(
"Cutelyst::ValidatorIn",
"The list of comparison values for the “%1” field is empty.").arg(_label);
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
QMap< QString, QString > ParamsMultiMap
ValidatorIn(const QString &field, const QVariant &values, Qt::CaseSensitivity cs=Qt::CaseSensitive, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new in validator.
Stores custom error messages and the input field label.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Base class for all validator rules.
QString field() const
Returns the name of the field to validate.
Checks if the field value is one from a list of values.
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 genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
~ValidatorIn() override
Deconstructs the in validator.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if the list of comparison values is empty.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
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 stash(const QVariantHash &unite)