cutelyst  5.0.1
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-2025 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() + u"_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  const ParamsMultiMap &params,
46  ValidatorRtFn cb) const
47 {
48  cb(validate(c, params));
49 }
50 
52 {
53  Q_UNUSED(errorData)
54  const QString _label = label(c);
55  if (_label.isEmpty()) {
56  //% "Confirmation failed."
57  return c->qtTrId("cutelyst-valconfirmed-genvalerr");
58  } else {
59  //: %1 will be replaced by the field label
60  //% "The value in the “%1“ field has not been confirmed."
61  return c->qtTrId("cutelyst-valconfirmed-genvalerr-label").arg(_label);
62  }
63  return {};
64 }
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Stores custom error messages and the input field label.
void validateCb(Context *c, const ParamsMultiMap &params, ValidatorRtFn cb) const override
bool trimBefore() const noexcept
The Cutelyst Context.
Definition: context.h:42
bool isEmpty() const const
QString trimmed() const const
ValidatorReturnType validate(Context *c, const ParamsMultiMap &params) const override
The Cutelyst namespace holds all public Cutelyst API.
QString debugString(const Context *c) const
Base class for all validator rules.
QString field() const noexcept
ValidatorConfirmed(const QString &field, const ValidatorMessages &messages={})
QString value(const ParamsMultiMap &params) const
QString label(const Context *c) const
std::function< void(ValidatorReturnType &&result)> ValidatorRtFn
Void callback function for validator rules that processes the ValidatorReturnType.
Definition: validatorrule.h:82
QString validationError(Context *c, const QVariant &errorData={}) const
QString qtTrId(const char *id, int n=-1) const
Definition: context.h:658
Contains the result of a single input parameter validation.
Definition: validatorrule.h:52
QString arg(Args &&... args) const const
T value(const Key &key, const T &defaultValue) const const
void setValue(QVariant &&value)