19 #include "validatorfilesize_p.h"
26 ValidatorRule(*new ValidatorFileSizePrivate(field, option, min, max, messages, defValKey))
42 bool decimalPointFound =
false;
43 const QChar decimalPoint = locale.decimalPoint();
46 bool byteSignFound =
false;
49 for (
const QChar &ch :
value) {
51 const ushort &uc = ch.unicode();
52 if (((uc > 47) && (uc < 58)) || (ch == decimalPoint)) {
53 if (startsWith == 0) {
56 if (ch == decimalPoint) {
57 if (decimalPointFound) {
61 decimalPointFound =
true;
64 if ((symbolPart.isEmpty() && (startsWith < 0)) || (!symbolPart.isEmpty() && (startsWith > 0))) {
70 }
else if ((uc != 9) && (uc != 32)) {
71 if (startsWith == 0) {
74 if ((digitPart.isEmpty() && (startsWith > 0)) || (!digitPart.isEmpty() && (startsWith < 0))) {
83 symbolPart.append(ch);
94 symbolPart.append(ch);
101 if (multiplier > 0) {
105 symbolPart.append(ch);
112 if (multiplier > 0) {
116 symbolPart.append(ch);
123 if (multiplier > 0) {
127 symbolPart.append(ch);
134 if (multiplier > 0) {
138 symbolPart.append(ch);
145 if (multiplier > 0) {
149 symbolPart.append(ch);
156 if (multiplier > 0) {
160 symbolPart.append(ch);
167 if ((multiplier == 0) || binary) {
171 symbolPart.append(ch);
181 byteSignFound =
true;
182 symbolPart.append(ch);
215 double size = locale.toDouble(digitPart, &ok);
219 if (multiplier > 0) {
220 const double _mult = binary ? std::exp2(multiplier * 10) : std::pow(10.0, multiplier * 3);
223 if ((min >= 1.0) && (size < min)) {
226 if ((max >= 1.0) && (size > max)) {
229 if (valid && fileSize) {
244 const QString v =
value(params);
251 if (d->min.isValid()) {
252 min = d->extractDouble(c, params, d->min, &ok);
258 if (ok && d->max.isValid()) {
259 max = d->extractDouble(c, params, d->max, &ok);
268 if (size <
static_cast<double>(std::numeric_limits<qulonglong>::max())) {
269 result.
value.setValue<qulonglong>(
static_cast<qulonglong
>(size + 0.5));
271 result.
value.setValue<
double>(size);
290 const QString _label =
label(c);
291 if (d->min.isValid() || d->max.isValid()) {
292 if (_label.isEmpty()) {
293 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"Invalid file size or file size not within the allowed limits.");
295 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);
298 if (_label.isEmpty()) {
299 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"Invalid file size.");
301 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The “%1” field does not contain a valid file size.").arg(_label);
312 const QString _label =
label(c);
314 const int sizeType = errorData.toInt();
317 if (_label.isEmpty()) {
318 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The minimum file size comparison value is not valid.");
320 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The minimum file size comparison value for the “%1” field is not valid.").arg(_label);
323 if (_label.isEmpty()) {
324 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The maximum file size comparison value is not valid.");
326 error = c->
translate(
"Cutelyst::ValidatorFileSize",
"The maximum file size comparison value for the “%1” field is not valid.").arg(_label);
336 c->
setStash(stashKey, c->
locale().textDirection() == Qt::LeftToRight ? QStringLiteral(
"^\\d+[,.٫]?\\d*\\s*[KkMmGgTt]?[Ii]?[Bb]?") : QStringLiteral(
"[KkMmGgTt]?[Ii]?[Bb]?\\s*\\d+[,.٫]?\\d*"));
QMap< QString, QString > ParamsMultiMap
QString genericValidationDataError(Context *c, const QVariant &errorData) const override
Returns a generic error messages if validation data is missing or invalid.
~ValidatorFileSize() override
Deconstructs the file size validator.
Stores custom error messages and the input field label.
QString value(const ParamsMultiMap ¶ms) const
Returns the value of the field from the input params.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
Option
Options for ValidatorFileSize.
void setStash(const QString &key, const QVariant &value)
Base class for all validator rules.
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.
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Checks if the input field contains a valid file size string like 1.5 GB.
Contains the result of a single input parameter validation.
The Cutelyst namespace holds all public Cutelyst API.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic 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.
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
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 ...