6#include "validatorrequiredif_p.h"
11 const QString &otherField,
12 const QStringList &otherValues,
14 :
ValidatorRule(*new ValidatorRequiredIfPrivate(field, otherField, otherValues, messages))
26 if (d->otherField.isEmpty() || d->otherValues.empty()) {
28 qCWarning(C_VALIDATOR).noquote() <<
debugString(c) <<
"Invalid validation data";
30 const QString v =
value(params);
32 trimBefore() ? params.value(d->otherField).trimmed() : params.value(d->otherField);
33 if (d->otherValues.contains(ov)) {
36 qCDebug(C_VALIDATOR).noquote().nospace()
37 <<
debugString(c) <<
" The field is not present or empty but \""
38 << d->otherField <<
"\" contains " << ov;
40 result.
value.setValue(v);
44 result.
value.setValue(v);
56 const QString _label =
label(c);
57 if (_label.isEmpty()) {
58 error = c->
translate(
"Cutelyst::ValidatorRequiredIf",
"This is required.");
61 error = c->
translate(
"Cutelyst::ValidatorRequiredIf",
"The “%1” field is required.")
QString translate(const char *context, const char *sourceText, const char *disambiguation=nullptr, int n=-1) const
The field under validation must be present and not empty if the other field is equal to any value in ...
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override
Performs the validation and returns the result.
~ValidatorRequiredIf() override
Deconstructs the required if validator.
QString genericValidationError(Context *c, const QVariant &errorData=QVariant()) const override
Returns a generic error message if validation failed.
ValidatorRequiredIf(const QString &field, const QString &otherField, const QStringList &otherValues, const ValidatorMessages &messages=ValidatorMessages())
Constructs a new required if validator.
Base class for all validator rules.
QString label(Context *c) const
Returns the human readable field label used for generic error messages.
bool trimBefore() const noexcept
Returns true if the field value should be trimmed before validation.
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.
QString debugString(Context *c) const
Returns a string that can be used for debug output if validation fails.
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.