Cutelyst  3.5.0
validatorrequiredunlessstash.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2018-2022 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorrequiredunlessstash_p.h"
7 
8 using namespace Cutelyst;
9 
10 ValidatorRequiredUnlessStash::ValidatorRequiredUnlessStash(const QString &field, const QString &stashKey, const QVariantList &stashValues, const ValidatorMessages &messages) :
11  ValidatorRule(* new ValidatorRequiredUnlessStashPrivate(field, stashKey, stashValues, messages))
12 {
13 }
14 
16 {
17 }
18 
20 {
21  ValidatorReturnType result;
22 
24 
25  if (d->stashKey.isEmpty() || d->stashValues.empty()) {
27  qCWarning(C_VALIDATOR, "ValidatorRequiredUnlessStash: invalid validation data for field %s at %s::%s", qPrintable(field()), qPrintable(c->controllerName()), qPrintable(c->actionName()));
28  } else {
29  const QString v = value(params);
30  const QVariant sv = c->stash(d->stashKey);
31  if (!d->stashValues.contains(sv)) {
32  if (!v.isEmpty()) {
33  result.value.setValue(v);
34  } else {
35  result.errorMessage = validationError(c);
36  }
37  } else {
38  if (!v.isEmpty()) {
39  result.value.setValue(v);
40  }
41  }
42  }
43 
44  return result;
45 }
46 
48 {
49  QString error;
50  Q_UNUSED(errorData)
51  const QString _label = label(c);
52  if (_label.isEmpty()) {
53  error = c->translate("Cutelyst::ValidatorRequiredUnlessStash", "This is required.");
54  } else {
55  //: %1 will be replaced by the field label
56  error = c->translate("Cutelyst::ValidatorRequiredUnlessStash", "The “%1” field is required.").arg(_label);
57  }
58  return error;
59 }
ValidatorRequiredUnlessStash(const QString &field, const QString &stashKey, const QVariantList &stashValues, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required unless stash validator.
QString validationError(Context *c, const QVariant &errorData=QVariant()) const
Returns a descriptive error message if validation failed.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
Stores custom error messages and the input field label.
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Performs the validation and returns the result.
The Cutelyst Context.
Definition: context.h:38
void stash(const QVariantHash &unite)
Definition: context.cpp:540
bool isEmpty() const const
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
Definition: context.cpp:471
QString validationDataError(Context *c, const QVariant &errorData=QVariant()) const
Returns an error message if any validation data is missing or invalid.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
void setValue(const T &value)
QString value(const ParamsMultiMap &params) const
Returns the value of the field from the input params.
The field under validation must be present and not emptly unless the content of a stash key is equal ...
QString field() const
Returns the name of the field to validate.
~ValidatorRequiredUnlessStash() override
Deconstructs the required unless stash validator.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const