Cutelyst
2.13.0
|
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()) | |
Constructs a new file size validator. More... | |
~ValidatorFileSize () override | |
Deconstructs the file size validator. | |
![]() | |
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 ¶ms) const override |
Performs the validation and returns the result. More... | |
![]() | |
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 ¶ms) const |
Returns the value of the field from the input params. | |
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 63 of file validatorfilesize.h.
enum Cutelyst::ValidatorFileSize::Option : quint8 |
Definition at line 69 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() |
||
) |
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 25 of file validatorfilesize.cpp.
|
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().
|
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.
c | Pointer to the current context. |
stashKey | Name 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().
|
overrideprotectedvirtual |
If validation succeeded, ValidatorReturnType::value will contain the input paramter value either as qulonglong or double.
Implements Cutelyst::ValidatorRule.
Definition at line 238 of file validatorfilesize.cpp.
References Cutelyst::ValidatorRule::defaultValue(), Cutelyst::ValidatorReturnType::errorMessage, Cutelyst::Context::locale(), validate(), Cutelyst::ValidatorRule::validationDataError(), Cutelyst::ValidatorRule::validationError(), Cutelyst::ValidatorReturnType::value, and Cutelyst::ValidatorRule::value().