cutelyst  4.4.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
validatorconfirmed.cpp
1 /*
2  * SPDX-FileCopyrightText: (C) 2017-2023 Matthias Fehring <mf@huessenbergnetz.de>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #include "validatorconfirmed_p.h"
7 
8 using namespace Cutelyst;
9 
11  : ValidatorRule(*new ValidatorConfirmedPrivate(field, messages))
12 {
13 }
14 
16 
18 {
19  ValidatorReturnType result;
20 
21  const QString v = value(params);
22 
23  if (!v.isEmpty()) {
24  const QString ofn = field() + QLatin1String("_confirmation");
25  QString ofv = params.value(ofn);
26 
27  if (trimBefore()) {
28  ofv = ofv.trimmed();
29  }
30 
31  if (Q_UNLIKELY(v != ofv)) {
32  result.errorMessage = validationError(c);
33  qCDebug(C_VALIDATOR).noquote().nospace()
34  << debugString(c) << " The value in \"" << ofn << "\" does not fit: \"" << v
35  << "\" != \"" << ofv << "\"";
36  } else {
37  result.value.setValue(v);
38  }
39  }
40 
41  return result;
42 }
43 
45 {
46  QString error;
47  Q_UNUSED(errorData)
48  const QString _label = label(c);
49  if (_label.isEmpty()) {
50  //% "Confirmation failed."
51  return c->qtTrId("cutelyst-valconfirmed-genvalerr");
52  } else {
53  //: %1 will be replaced by the field label
54  //% "The value in the “%1“ field has not been confirmed."
55  return c->qtTrId("cutelyst-valconfirmed-genvalerr-label").arg(_label);
56  }
57  return error;
58 }
The Cutelyst Context.
Definition: context.h:42
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:656
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
ValidatorConfirmed(const QString &field, const ValidatorMessages &messages=ValidatorMessages())
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
Base class for all validator rules.
QString field() const noexcept
QString validationError(Context *c, const QVariant &errorData={}) const
QString label(Context *c) const
bool trimBefore() const noexcept
QString value(const ParamsMultiMap &params) const
QString debugString(Context *c) const
The Cutelyst namespace holds all public Cutelyst API.
T value(const Key &key, const T &defaultValue) const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QString trimmed() const const
void setValue(QVariant &&value)
Stores custom error messages and the input field label.
Contains the result of a single input parameter validation.
Definition: validatorrule.h:49