cutelyst
4.5.1
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
|
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, QStringList > | errors () 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 | |
ValidatorResult & | operator= (const ValidatorResult &other) noexcept |
ValidatorResult & | operator= (ValidatorResult &&other) noexcept |
QVariant | value (const QString &field) const noexcept |
QVariantHash | values () const noexcept |
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.
Validity is simply determined by the fact, that it does not contain any error information.
Definition at line 71 of file validatorresult.h.
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.
|
defaultnoexcept |
Constructs a copy of other.
|
defaultnoexcept |
Move-constructs a ValidatorResult instance, making it point at the same object that other was pointing to.
|
defaultnoexcept |
Destroys the ValidatorResult object.
Adds new error information to the internal QHash.
field | Name of the input parameter that has validation errors. |
message | Error message shown to the user. |
Definition at line 33 of file validatorresult.cpp.
References QList::append().
Referenced by Cutelyst::Validator::validate().
Adds new extra data that came up when validating the input field.
Definition at line 113 of file validatorresult.cpp.
References extra().
Referenced by Cutelyst::Validator::validate().
Adds a new value extracted from the specified input field.
Definition at line 98 of file validatorresult.cpp.
References value().
Referenced by Cutelyst::Validator::validate().
|
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().
|
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().
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.
Definition at line 68 of file validatorresult.cpp.
References QJsonArray::fromStringList(), and QJsonObject::insert().
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().
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.
Definition at line 108 of file validatorresult.cpp.
References extras().
Referenced by addExtra().
|
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.
Definition at line 103 of file validatorresult.cpp.
Referenced by extra().
QStringList ValidatorResult::failedFields | ( | ) | const |
Definition at line 83 of file validatorresult.cpp.
|
noexcept |
Returns true
if the field has validation errors.
Definition at line 63 of file validatorresult.cpp.
References errors().
|
noexcept |
Returns true
if the validation was successful.
Definition at line 28 of file validatorresult.cpp.
References errors().
|
inlineexplicitnoexcept |
Returns true
if the validation was successful.
Definition at line 177 of file validatorresult.h.
|
defaultnoexcept |
Assigns other to this ValidatorResult and returns a reference to this result.
|
defaultnoexcept |
Move-assings other to this ValidatorResult instance.
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.
Definition at line 93 of file validatorresult.cpp.
References values().
Referenced by addValue().
|
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.
Definition at line 88 of file validatorresult.cpp.
Referenced by value().