Cutelyst  2.13.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
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:
Inheritance graph
[legend]

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())
 Constructs a new file size validator. More...
 
 ~ValidatorFileSize () override
 Deconstructs the file size validator.
 
- Public Member Functions inherited from Cutelyst::ValidatorRule
 ValidatorRule (const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString())
 Constructs a new ValidatorRule with the given parameters. More...
 
virtual ~ValidatorRule ()
 Deconstructs the ValidatorRule.
 

Static Public Member Functions

static void inputPattern (Context *c, const QString &stashKey=QStringLiteral("fileSizePattern"))
 Puts an HTML input pattern for file sizes into the stash. More...
 
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
 Returns a generic error messages if validation data is missing or invalid. More...
 
QString genericValidationError (Context *c, const QVariant &errorData=QVariant()) const override
 Returns a generic error message if validation failed.
 
ValidatorReturnType validate (Context *c, const ParamsMultiMap &params) const override
 Performs the validation and returns the result. More...
 
- Protected Member Functions inherited from Cutelyst::ValidatorRule
 ValidatorRule (ValidatorRulePrivate &dd)
 Constructs a new ValidatorRule object with the given private class. More...
 
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 and put it into the result. More...
 
QString field () const
 Returns the name of the field to validate. More...
 
virtual QString genericParsingError (Context *c, const QVariant &errorData=QVariant()) const
 Returns a generic error message if an error occures while parsing input. More...
 
QString label (Context *c) const
 Returns the human readable field label used for generic error messages. More...
 
QString parsingError (Context *c, const QVariant &errorData=QVariant()) const
 Returns an error message if an error occured while parsing input. More...
 
bool trimBefore () const
 Returns true if the field value should be trimmed before validation. More...
 
QString validationDataError (Context *c, const QVariant &errorData=QVariant()) const
 Returns an error message if any validation data is missing or invalid. More...
 
QString validationError (Context *c, const QVariant &errorData=QVariant()) const
 Returns a descriptive error message if validation failed. More...
 
QString value (const ParamsMultiMap &params) const
 Returns the value of the field from the input params.
 

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().

See also
Validator for general usage of validators.
Since
Cutelyst 2.0.0

Definition at line 63 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 69 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() 
)
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 25 of file validatorfilesize.cpp.

Member Function Documentation

◆ genericValidationDataError()

QString ValidatorFileSize::genericValidationDataError ( Context c,
const QVariant &  errorData 
) const
overrideprotectedvirtual

errorData will contain either 0 if the minimum size value is invalid, or 1 if the maximum size value is invalid.

Reimplemented from Cutelyst::ValidatorRule.

Definition at line 308 of file validatorfilesize.cpp.

References Cutelyst::ValidatorRule::label(), and Cutelyst::Context::translate().

◆ inputPattern()

void ValidatorFileSize::inputPattern ( Context c,
const QString &  stashKey = QStringLiteral("fileSizePattern") 
)
static

This will either put "^\\d+[,.٫]?\\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+[,.٫]?\\d*" if the direction is right to left.

Parameters
cPointer to the current context.
stashKeyName of the stash key to put the pattern in.

Definition at line 333 of file validatorfilesize.cpp.

References Cutelyst::Context::locale(), and Cutelyst::Context::setStash().

◆ validate()

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