6#include "validatorfilesize_p.h"
18 const QString &defValKey)
19 :
ValidatorRule(*new ValidatorFileSizePrivate(field, option, min, max, messages, defValKey))
31 const QLocale &locale,
38 bool decimalPointFound =
false;
39 const QString decimalPoint(locale.decimalPoint());
42 bool byteSignFound =
false;
45 for (
const QChar &ch :
value) {
47 const ushort &uc = ch.unicode();
48 if (((uc > 47) && (uc < 58)) || (ch == decimalPoint)) {
49 if (startsWith == 0) {
52 if (ch == decimalPoint) {
53 if (decimalPointFound) {
57 decimalPointFound =
true;
60 if ((symbolPart.isEmpty() && (startsWith < 0)) ||
61 (!symbolPart.isEmpty() && (startsWith > 0))) {
67 }
else if ((uc != 9) &&
69 if (startsWith == 0) {
72 if ((digitPart.isEmpty() && (startsWith > 0)) ||
73 (!digitPart.isEmpty() && (startsWith < 0))) {
82 symbolPart.append(ch);
92 symbolPart.append(ch);
102 symbolPart.append(ch);
108 if (multiplier > 0) {
112 symbolPart.append(ch);
118 if (multiplier > 0) {
122 symbolPart.append(ch);
128 if (multiplier > 0) {
132 symbolPart.append(ch);
138 if (multiplier > 0) {
142 symbolPart.append(ch);
148 if (multiplier > 0) {
152 symbolPart.append(ch);
158 if ((multiplier == 0) || binary) {
162 symbolPart.append(ch);
171 byteSignFound =
true;
172 symbolPart.append(ch);
204 double size = locale.toDouble(digitPart, &ok);
208 if (multiplier > 0) {
210 binary ? std::exp2(multiplier * 10) : std::pow(10.0, multiplier * 3);
213 if ((min >= 1.0) && (size < min)) {
216 if ((max >= 1.0) && (size > max)) {
219 if (valid && fileSize) {
234 const QString v =
value(params);
241 if (d->min.isValid()) {
242 min = d->extractDouble(c, params, d->min, &ok);
248 if (ok && d->max.isValid()) {
249 max = d->extractDouble(c, params, d->max, &ok);
258 if (size <
static_cast<double>(std::numeric_limits<qulonglong>::max())) {
259 result.
value.setValue<qulonglong>(
static_cast<qulonglong
>(size + 0.5));
261 result.
value.setValue(size);
280 const QString _label =
label(c);
281 if (d->min.isValid() || d->max.isValid()) {
282 if (_label.isEmpty()) {
283 error = c->
translate(
"Cutelyst::ValidatorFileSize",
284 "Invalid file size or file size not within the allowed limits.");
286 error = c->
translate(
"Cutelyst::ValidatorFileSize",
287 "The value in the “%1” field is either not a valid file size or "
288 "not within the allowed limits.")
292 if (_label.isEmpty()) {
293 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"Invalid file size.");
295 error = c->
translate(
"Cutelyst::ValidatorFileSize",
296 "The “%1” field does not contain a valid file size.")
308 const QString _label =
label(c);
310 const int sizeType = errorData.toInt();
313 if (_label.isEmpty()) {
314 error = c->
translate(
"Cutelyst::ValidatorFileSize",
315 "The minimum file size comparison value is not valid.");
318 "Cutelyst::ValidatorFileSize",
319 "The minimum file size comparison value for the “%1” field is not valid.")
323 if (_label.isEmpty()) {
324 error = c->
translate(
"Cutelyst::ValidatorFileSize",
325 "The maximum file size comparison value is not valid.");
328 "Cutelyst::ValidatorFileSize",
329 "The maximum file size comparison value for the “%1” field is not valid.")
341 c->
locale().textDirection() == Qt::LeftToRight
342 ? QStringLiteral(
"^\\d+[,.٫]?\\d*\\s*[KkMmGgTt]?[Ii]?[Bb]?")
343 : QStringLiteral(
"[KkMmGgTt]?[Ii]?[Bb]?\\s*\\d+[,.٫]?\\d*"));
QLocale locale() const noexcept
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
void setStash(const QString &key, const QVariant &value)
Checks if the input field contains a valid file size string like 1.5 GB.
~ValidatorFileSize() override
Deconstructs the file size validator.
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if validation data is missing or invalid.
Option
Options for ValidatorFileSize.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
static void inputPattern(Context *c, const QString &stashKey=QStringLiteral("fileSizePattern"))
Puts an HTML input pattern for file sizes into the stash.
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.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
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 ...
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
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.
The Cutelyst namespace holds all public Cutelyst API.
QMultiMap< QString, QString > ParamsMultiMap
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.