cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Cutelyst::ValidatorFileSize Class Reference

Checks if the input field contains a valid file size string like 1.5 GB. More...

#include <Cutelyst/Plugins/Utils/validatorfilesize.h>

Inheritance diagram for Cutelyst::ValidatorFileSize:

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.
 

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 &params) 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 &params) const
 

Detailed Description

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.

Note
The conversion of big sizes like Petabyte and above might lead to rounding issues in the return value.
Unless validation is started with NoTrimming, whitespaces will be removed from the beginning and the end of the input value before validation. If the field's value is empty or if the field is missing in the input data, the validation will succeed without performing the validation itself. Use one of the required validators to require the field to be present and not empty.

If you want to use a pattern in your HTML input element that matches this validator, use ValidatorFileSize::inputPattern().

Return type
On success, ValidatorReturnType::value will contain the file size in bytes as either qulonglong (if that fits) or as double.
See also
Validator for general usage of validators.
Since
Cutelyst 2.0.0

Definition at line 61 of file validatorfilesize.h.

Member Enumeration Documentation

◆ Option

Enumerator
NoOption 

No option to use.

OnlyBinary 

Accepts only binary prefix symbols like KiB, MiB, etc.

OnlyDecimal 

Accepts only decimal prefix symbols like kB, MB, etc.

ForceBinary 

Forces the usage of the binary system when generating the return value but will not fail if the prefix symbol is decimal.

ForceDecimal 

Forces the usage of the decimal system when generating the return value but will not fail if the prefix symbol is binray.

Definition at line 67 of file validatorfilesize.h.

Constructor & Destructor Documentation

◆ ValidatorFileSize()

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.

Parameters
fieldName of the input field to validate.
optionOption to use when validating and generating the return value.
minOptional 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.
maxOptional 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.
messagesCustom error messages if validation fails.
defValKeyStash key containing a default value if input field is empty. This value will NOT be validated.

Definition at line 13 of file validatorfilesize.cpp.

Member Function Documentation

◆ genericValidationDataError()

QString ValidatorFileSize::genericValidationDataError ( Context c,
const QVariant errorData 
) const
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().

◆ genericValidationError()

QString ValidatorFileSize::genericValidationError ( Context c,
const QVariant errorData = QVariant() 
) const
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().

◆ inputPattern()

void ValidatorFileSize::inputPattern ( Context c,
const QString stashKey = QStringLiteral("fileSizePattern") 
)
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().

Parameters
cPointer to the current context.
stashKeyName 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().

◆ validate()

ValidatorReturnType ValidatorFileSize::validate ( Context c,
const ParamsMultiMap params 
) const
overrideprotectedvirtual