cutelyst
4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
|
Checks if the input field contains a valid file size string like 1.5 GB. More...
#include <Cutelyst/Plugins/Utils/validatorfilesize.h>
Public Types | |
enum | Option : quint8 { NoOption , OnlyBinary , OnlyDecimal , ForceBinary , ForceDecimal } |
Options for ValidatorFileSize. More... | |
Public Member Functions | |
ValidatorFileSize (const QString &field, Option option=NoOption, const QVariant &min=QVariant(), const QVariant &max=QVariant(), const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString()) | |
~ValidatorFileSize () override | |
Deconstructs the file size validator. | |
Public Member Functions inherited from Cutelyst::ValidatorRule | |
ValidatorRule (const QString &field, const ValidatorMessages &messages={}, const QString &defValKey={}, QByteArrayView validatorName=nullptr) | |
virtual | ~ValidatorRule () |
Deconstructs the ValidatorRule. | |
Static Public Member Functions | |
static void | inputPattern (Context *c, const QString &stashKey=QStringLiteral("fileSizePattern")) |
static bool | validate (const QString &value, double min=-1, double max=-1, Option option=NoOption, const QLocale &locale=QLocale(), double *fileSize=nullptr) |
Returns true if value is a valid file size string. More... | |
Protected Member Functions | |
QString | genericValidationDataError (Context *c, const QVariant &errorData) const override |
QString | genericValidationError (Context *c, const QVariant &errorData=QVariant()) const override |
ValidatorReturnType | validate (Context *c, const ParamsMultiMap ¶ms) const override |
Protected Member Functions inherited from Cutelyst::ValidatorRule | |
ValidatorRule (ValidatorRulePrivate &dd) | |
QString | debugString (Context *c) const |
void | defaultValue (Context *c, ValidatorReturnType *result) const |
QString | field () const noexcept |
virtual QString | genericParsingError (Context *c, const QVariant &errorData={}) const |
QString | label (Context *c) const |
QString | parsingError (Context *c, const QVariant &errorData={}) const |
bool | trimBefore () const noexcept |
QString | validationDataError (Context *c, const QVariant &errorData={}) const |
QString | validationError (Context *c, const QVariant &errorData={}) const |
QString | value (const ParamsMultiMap ¶ms) const |
The field under validation is ownly allowed to contain a size number and a prefix symbol for either binary (MiB, etc.) or decimal (MB, etc.) file sizes. Negative values are not allowed. White space (space and tab) is allowed and will be ignored by the validation logic. Size prefixes from kB to YiB are supported.
This validation rule is locale aware, so be sure to set the correct locale on your context. The locale awareness is especially important to select the correct decimal separator sign. To support both language directions, the validation logic supports both positions of the prefix symbol for all languages (e.g. "KB 2" and "2 KB" are handled the same way and are both valid).
Unless a specific option has been selected, the validator will use binary or decimal base according to the prefix. (2KiB will return 2048, 2kB will return 2000). Recognizion of the prefix is case insensitive.
If you want to use a pattern in your HTML input element that matches this validator, use ValidatorFileSize::inputPattern().
Definition at line 59 of file validatorfilesize.h.
enum Cutelyst::ValidatorFileSize::Option : quint8 |
Definition at line 65 of file validatorfilesize.h.
ValidatorFileSize::ValidatorFileSize | ( | const QString & | field, |
Option | option = NoOption , |
||
const QVariant & | min = QVariant() , |
||
const QVariant & | max = QVariant() , |
||
const ValidatorMessages & | messages = ValidatorMessages() , |
||
const QString & | defValKey = QString() |
||
) |
Constructs a new ValidatorFileSize object witht the given parameters.
field | Name of the input field to validate. |
option | Option to use when validating and generating the return value. |
min | Optional minimum size, can be either a number value to compare against or the name of a stash key that contains the value. To disable, use a value lower 0 or an invalid QVariant. |
max | Optional maximum size, can be either a number value to compare against or the name of a stash key that contains the value. To disable, use a value lower 0 or an invalid QVariant. |
messages | Custom error messages if validation fails. |
defValKey | Stash key containing a default value if input field is empty. This value will NOT be validated. |
Definition at line 13 of file validatorfilesize.cpp.
|
overrideprotectedvirtual |
Returns a generic error messages if validation data is missing or invalid.
Reimplemented from Cutelyst::ValidatorRule.
Definition at line 342 of file validatorfilesize.cpp.
References QString::arg(), QString::isEmpty(), Cutelyst::ValidatorRule::label(), Cutelyst::Context::qtTrId(), and QVariant::toInt().
|
overrideprotectedvirtual |
Returns a generic error message if validation failed.
Reimplemented from Cutelyst::ValidatorRule.
Definition at line 317 of file validatorfilesize.cpp.
References QString::arg(), QString::isEmpty(), Cutelyst::ValidatorRule::label(), and Cutelyst::Context::qtTrId().
|
static |
Puts an HTML input pattern for file sizes into the stash.
This will either put "^\\d+[%1]?\\d*\\s*[KkMmGgTt]?[Ii]?[Bb]?"
into the stashKey if the current locale’s direction is from left to right, or "[KkMmGgTt]?[Ii]?[Bb]?\\s*\\d+[%1]?\\d*"
if the direction is right to left, where %1
will be replaced by the return value of QLocale::decimalPoint().
c | Pointer to the current context. |
stashKey | Name of the stash key to put the pattern in. |
Definition at line 384 of file validatorfilesize.cpp.
References QLocale::decimalPoint(), Qt::LeftToRight, Cutelyst::Context::locale(), Cutelyst::Context::setStash(), and QLocale::textDirection().
|
overrideprotectedvirtual |
Performs the validation on the input params and returns the result.
If validation succeeded, ValidatorReturnType::value will contain the file size in bytes either as qulonglong (if that fits) or double.
Implements Cutelyst::ValidatorRule.
Definition at line 262 of file validatorfilesize.cpp.
References Cutelyst::ValidatorRule::debugString(), Cutelyst::ValidatorRule::defaultValue(), Cutelyst::ValidatorReturnType::errorMessage, QString::isEmpty(), Cutelyst::Context::locale(), QVariant::setValue(), validate(), Cutelyst::ValidatorRule::validationDataError(), Cutelyst::ValidatorRule::validationError(), Cutelyst::ValidatorReturnType::value, and Cutelyst::ValidatorRule::value().