19 #include "validatordigits_p.h"
21 using namespace Cutelyst;
24 ValidatorRule(*new ValidatorDigitsPrivate(field, length, messages, defValKey))
40 int _length = d->extractInt(c, params, d->length, &ok);
49 if ((_length > 0) && (v.
length() != _length)) {
51 qCDebug(C_VALIDATOR,
"ValidatorDigits: Validation failed for value \"%s\" in field %s at %s::%s: does not contain exactly %i digit(s).", qPrintable(v), qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()), _length);
57 qCDebug(C_VALIDATOR,
"ValidatorDigits: Validation failed for value \"%s\" in field %s at %s::%s: does not only contain digits.", qPrintable(v), qPrintable(
field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
71 for (
const QChar &ch : value) {
72 const ushort &uc = ch.unicode();
73 if (!((uc > 47) && (uc < 58))) {
79 if (valid && (length > 0) && (length != value.length())) {
91 const int _length = errorData.
toInt();
95 error = c->
translate(
"Cutelyst::ValidatorDigits",
"Must contain exactly %n digit(s).",
"", _length);
97 error = c->
translate(
"Cutelyst::ValidatorDigits",
"Must only contain digits.");
102 error = c->
translate(
"Cutelyst::ValidatorDigits",
"The “%1” field must contain exactly %n digit(s).",
"", _length).
arg(_label);
105 error = c->
translate(
"Cutelyst::ValidatorDigits",
"The “%1” field must only contain digits.").
arg(_label);
QString field() const
Returns the name of the field to validate.
Checks for digits only with optional length check.
~ValidatorDigits() override
Deconstructs the digits validator.
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.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error if validation failed.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
int toInt(bool *ok) const
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Base class for all validator rules.
static bool validate(const QString &value, int length=-1)
Returns true if value only contains digits.
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)
ValidatorDigits(const QString &field, const QVariant &length=-1, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
Constructs a new digits validator.
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