cutelyst  4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Cutelyst::ValidatorResult Class Reference

Provides information about performed validations. More...

#include <Cutelyst/Plugins/Utils/ValdatorResult>

Public Member Functions

 ValidatorResult ()
 
 ValidatorResult (const ValidatorResult &other) noexcept
 
 ValidatorResult (ValidatorResult &&other) noexcept
 
 ~ValidatorResult () noexcept
 
void addError (const QString &field, const QString &message)
 
void addExtra (const QString &field, const QVariant &extra)
 
void addValue (const QString &field, const QVariant &value)
 
QHash< QString, QStringListerrors () const noexcept
 
QStringList errors (const QString &field) const noexcept
 
QJsonObject errorsJsonObject () const
 
QStringList errorStrings () const
 
QVariant extra (const QString &field) const noexcept
 
QVariantHash extras () const noexcept
 
QStringList failedFields () const
 Returns a list of fields with errors. More...
 
bool hasErrors (const QString &field) const noexcept
 
bool isValid () const noexcept
 
 operator bool () const noexcept
 
ValidatorResultoperator= (const ValidatorResult &other) noexcept
 
ValidatorResultoperator= (ValidatorResult &&other) noexcept
 
QVariant value (const QString &field) const noexcept
 
QVariantHash values () const noexcept
 

Detailed Description

ValidatorResult will be returned by Validator when calling validate() on it. It contains information about occurred validation errors, like the error strings of each failed validator and a list of fields where validation failed.

Additionally to the error messages that occure if validation fails for one or more fields, ValidatorResult will also contain the extracted values from the input parameters. Use values() to return all values or value() to return the value for a single field. Because there will be only one value stored for each field, you should order your validators in a way that a validator for a field comes last that converts the input QString into the required type. See the documentation for the specific validator to see what type of data it returns.

Some validators might even return more details about the validation result. This extra data can be returned with the extras() method for all input parameters or with extra() for a single one.

Beside the isValid() function, that returns true if the complete validation process was successful and false if any of the validators failed, ValidatorResult provides a bool operator that makes it usable in if statements.

void MyController:do_form(Context *c)
{
static Validator v({
new ValidatorRequired(QStringLiteral("birthday"),
new ValidatorDate(QStringLiteral("birthday")
});
ValidatorResult r = v.validate(c);
if (r) {
// do stuff if validation was successful
auto extractedValues = r.values();
} else {
// do other stuff if validation failed
auto errors = r.errors();
}
// you can also compare the boolen value of the result directly
// for example together with the Validator::FillStashOnError flag
// to automatically fill the stash with error information and input values
if (v.validate(Validator::FillStashOnError)) {
// do stuff if validation was successful
}
}
QHash< QString, QStringList > errors() const noexcept

Validity is simply determined by the fact, that it does not contain any error information.

Definition at line 71 of file validatorresult.h.

Constructor & Destructor Documentation

◆ ValidatorResult() [1/3]

ValidatorResult::ValidatorResult ( )

Constructs a new ValidatorResult object.

A newly constructed ValidatorResult willl be valid by default, because it does not contain any error information.

Definition at line 13 of file validatorresult.cpp.

◆ ValidatorResult() [2/3]

ValidatorResult::ValidatorResult ( const ValidatorResult other)
defaultnoexcept

Constructs a copy of other.

◆ ValidatorResult() [3/3]

ValidatorResult::ValidatorResult ( ValidatorResult &&  other)
defaultnoexcept

Move-constructs a ValidatorResult instance, making it point at the same object that other was pointing to.

◆ ~ValidatorResult()

ValidatorResult::~ValidatorResult ( )
defaultnoexcept

Destroys the ValidatorResult object.

Member Function Documentation

◆ addError()

void ValidatorResult::addError ( const QString field,
const QString message 
)

Adds new error information to the internal QHash.

Parameters
fieldName of the input parameter that has validation errors.
messageError message shown to the user.
See also
errorString() errors() hasErrors()

Definition at line 33 of file validatorresult.cpp.

References QList::append().

Referenced by Cutelyst::Validator::validate().

◆ addExtra()

void ValidatorResult::addExtra ( const QString field,
const QVariant extra 
)

Adds new extra data that came up when validating the input field.

See also
extras() extra()
Since
Cutelyst 2.0.0

Definition at line 113 of file validatorresult.cpp.

References extra().

Referenced by Cutelyst::Validator::validate().

◆ addValue()

void ValidatorResult::addValue ( const QString field,
const QVariant value 
)

Adds a new value extracted from the specified input field.

See also
values() value()
Since
Cutelyst 2.0.0

Definition at line 98 of file validatorresult.cpp.

References value().

Referenced by Cutelyst::Validator::validate().

◆ errors() [1/2]

QHash< QString, QStringList > ValidatorResult::errors ( ) const
noexcept

Returns a dictionary containing fields with errors.

The rerunted hash contains the name of the input parameter as key and a list of validation errors for this parameter in a QStringList as value.

Definition at line 53 of file validatorresult.cpp.

Referenced by errors(), hasErrors(), isValid(), and Cutelyst::Validator::validate().

◆ errors() [2/2]

QStringList ValidatorResult::errors ( const QString field) const
noexcept

Returns a list of all error messages for an input field.

If there were no errors for the field, the returned list will be empty.

Definition at line 58 of file validatorresult.cpp.

References errors().

◆ errorsJsonObject()

QJsonObject ValidatorResult::errorsJsonObject ( ) const

Returns the dictionray containing fields with errors as JSON object.

This returns the same data as errors() but converted into a JSON object that has the field names as keys and the values will be a JSON array of strings containing the errors for the field.

See also
errors() errorStrings()
Since
Cutelyst 1.12.0

Definition at line 68 of file validatorresult.cpp.

References QJsonArray::fromStringList(), and QJsonObject::insert().

◆ errorStrings()

QStringList ValidatorResult::errorStrings ( ) const

Returns a list of all error messages.

Definition at line 40 of file validatorresult.cpp.

References QList::append().

Referenced by Cutelyst::Validator::validate().

◆ extra()

QVariant ValidatorResult::extra ( const QString field) const
noexcept

Returns the extra data for the input field.

Returns a QVariant containing extra data generated by the validators. If the field does not have any extra data, a default constructed QVariant will be returned. Have a look at the documentation of the specific validators to see what kind of extra data they might generate.

See also
extras() addExtra()
Since
Cutelyst 2.0.0

Definition at line 108 of file validatorresult.cpp.

References extras().

Referenced by addExtra().

◆ extras()

QVariantHash ValidatorResult::extras ( ) const
noexcept

Returns all extra data that has been extracted by the validators.

Returns a QVariantHash where the key is the name of the input parameter and the value contains the extra data for that field. Have a look at the documentation of the specific validators to see what kind of extra data they might generate.

See also
extra() addExtra()
Since
Cutelyst 2.0.0

Definition at line 103 of file validatorresult.cpp.

Referenced by extra().

◆ failedFields()

QStringList ValidatorResult::failedFields ( ) const
Since
Cutelyst 1.12.0

Definition at line 83 of file validatorresult.cpp.

◆ hasErrors()

bool ValidatorResult::hasErrors ( const QString field) const
noexcept

Returns true if the field has validation errors.

Since
Cutelyst 2.0.0

Definition at line 63 of file validatorresult.cpp.

References errors().

◆ isValid()

bool ValidatorResult::isValid ( ) const
noexcept

Returns true if the validation was successful.

Note
A newly constructed ValidatorResult will be valid by default.

Definition at line 28 of file validatorresult.cpp.

References errors().

◆ operator bool()

Cutelyst::ValidatorResult::operator bool ( ) const
inlineexplicitnoexcept

Returns true if the validation was successful.

Note
A newly constructed ValidatorResult will be valid by default.

Definition at line 177 of file validatorresult.h.

◆ operator=() [1/2]

ValidatorResult & ValidatorResult::operator= ( const ValidatorResult other)
defaultnoexcept

Assigns other to this ValidatorResult and returns a reference to this result.

◆ operator=() [2/2]

ValidatorResult & ValidatorResult::operator= ( ValidatorResult &&  other)
defaultnoexcept

Move-assings other to this ValidatorResult instance.

◆ value()

QVariant ValidatorResult::value ( const QString field) const
noexcept

Returns the extracted value for the input field.

The returned value will be a QVariant. If there is no value for the field, the returned QVariant will be default constructed. Have a look at the documentation of the specific validator to see what kind of extracted value they will provide.

See also
values() addValue()
Since
Cutelyst 2.0.0

Definition at line 93 of file validatorresult.cpp.

References values().

Referenced by addValue().

◆ values()

QVariantHash ValidatorResult::values ( ) const
noexcept

Returns the values that have been extracted by the validators.

Returns a QVariantHash where the key is the name of the input parameter and the value contains the value extracted from the input parameter. Have a look at the documentation of the specific validator to see what kind of extracted value they will provide.

See also
value() addValue()
Since
Cutelyst 2.0.0

Definition at line 88 of file validatorresult.cpp.

Referenced by value().