6 #include "validatorfilesize_p.h"
13 ValidatorRule(*new ValidatorFileSizePrivate(field, option, min, max, messages, defValKey))
29 bool decimalPointFound =
false;
30 const QString decimalPoint(locale.decimalPoint());
33 bool byteSignFound =
false;
36 for (
const QChar &ch :
value) {
38 const ushort &uc = ch.unicode();
39 if (((uc > 47) && (uc < 58)) || (ch == decimalPoint)) {
40 if (startsWith == 0) {
43 if (ch == decimalPoint) {
44 if (decimalPointFound) {
48 decimalPointFound =
true;
51 if ((symbolPart.isEmpty() && (startsWith < 0)) || (!symbolPart.isEmpty() && (startsWith > 0))) {
57 }
else if ((uc != 9) && (uc != 32)) {
58 if (startsWith == 0) {
61 if ((digitPart.isEmpty() && (startsWith > 0)) || (!digitPart.isEmpty() && (startsWith < 0))) {
70 symbolPart.append(ch);
81 symbolPart.append(ch);
92 symbolPart.append(ch);
103 symbolPart.append(ch);
110 if (multiplier > 0) {
114 symbolPart.append(ch);
121 if (multiplier > 0) {
125 symbolPart.append(ch);
132 if (multiplier > 0) {
136 symbolPart.append(ch);
143 if (multiplier > 0) {
147 symbolPart.append(ch);
154 if ((multiplier == 0) || binary) {
158 symbolPart.append(ch);
168 byteSignFound =
true;
169 symbolPart.append(ch);
202 double size = locale.toDouble(digitPart, &ok);
206 if (multiplier > 0) {
207 const double _mult = binary ? std::exp2(multiplier * 10) : std::pow(10.0, multiplier * 3);
210 if ((min >= 1.0) && (size < min)) {
213 if ((max >= 1.0) && (size > max)) {
216 if (valid && fileSize) {
231 const QString v =
value(params);
238 if (d->min.isValid()) {
239 min = d->extractDouble(c, params, d->min, &ok);
245 if (ok && d->max.isValid()) {
246 max = d->extractDouble(c, params, d->max, &ok);
255 if (size <
static_cast<double>(std::numeric_limits<qulonglong>::max())) {
256 result.
value.setValue<qulonglong>(
static_cast<qulonglong
>(size + 0.5));
258 result.
value.setValue(size);
277 const QString _label =
label(c);
278 if (d->min.isValid() || d->max.isValid()) {
279 if (_label.isEmpty()) {
280 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"Invalid file size or file size not within the allowed limits.");
282 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The value in the “%1” field is either not a valid file size or not within the allowed limits.").arg(_label);
285 if (_label.isEmpty()) {
286 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"Invalid file size.");
288 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The “%1” field does not contain a valid file size.").arg(_label);
299 const QString _label =
label(c);
301 const int sizeType = errorData.toInt();
304 if (_label.isEmpty()) {
305 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The minimum file size comparison value is not valid.");
307 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The minimum file size comparison value for the “%1” field is not valid.").arg(_label);
310 if (_label.isEmpty()) {
311 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The maximum file size comparison value is not valid.");
313 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The maximum file size comparison value for the “%1” field is not valid.").arg(_label);
323 c->
setStash(stashKey, c->
locale().textDirection() == Qt::LeftToRight ? QStringLiteral(
"^\\d+[,.٫]?\\d*\\s*[KkMmGgTt]?[Ii]?[Bb]?") : 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.