Cutelyst
2.13.0
|
Provides information about performed validations. More...
#include <Cutelyst/Plugins/Utils/ValdatorResult>
Public Member Functions | |
ValidatorResult () | |
Constructs a new ValidatorResult. More... | |
ValidatorResult (const ValidatorResult &other) | |
Constructs a copy of other. | |
~ValidatorResult () | |
Deconstructs the ValidatorResult. | |
void | addError (const QString &field, const QString &message) |
Adds new error information to the internal QHash. More... | |
void | addExtra (const QString &field, const QVariant &extra) |
Adds new extra data that came up when validating the input field. More... | |
void | addValue (const QString &field, const QVariant &value) |
Adds a new value extracted from the specified input field. More... | |
QHash< QString, QStringList > | errors () const |
Returns a dictionary containing fields with errors. More... | |
QStringList | errors (const QString &field) const |
Returns a list of all error messages for an input field. More... | |
QJsonObject | errorsJsonObject () const |
Returns the dictionray containing fields with errors as JSON object. More... | |
QStringList | errorStrings () const |
Returns a list of all error messages. More... | |
QVariant | extra (const QString &field) const |
Returns the extra data for the input field. More... | |
QVariantHash | extras () const |
Returns all extra data that has been extracted by the validators. More... | |
QStringList | failedFields () const |
Returns a list of fields with errors. More... | |
bool | hasErrors (const QString &field) const |
Returns true if the field has validation errors. More... | |
bool | isValid () const |
Returns true if the validation was successful. More... | |
operator bool () const | |
Returns true if the validation was successful. More... | |
ValidatorResult & | operator= (const ValidatorResult &other) |
Assigns other to this ValidatorResult. | |
QVariant | value (const QString &field) const |
Returns the extracted value for the input field. More... | |
QVariantHash | values () const |
Returns the values that have been extracted by the validators. More... | |
ValidatorResult will be returned by Validator when calling validate() on it. It contains information about occured 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 wanted 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 81 of file validatorresult.h.
ValidatorResult::ValidatorResult | ( | ) |
A newly constructed ValidatorResult willl be valid by default, because it does not contain any error information.
Definition at line 25 of file validatorresult.cpp.
void ValidatorResult::addError | ( | const QString & | field, |
const QString & | message | ||
) |
field | Name of the input parameter that has validation errors. |
message | Error message shown to the user. |
Definition at line 50 of file validatorresult.cpp.
Referenced by Cutelyst::Validator::validate().
void ValidatorResult::addExtra | ( | const QString & | field, |
const QVariant & | extra | ||
) |
field | Name of the input parameter the extra data occured for. |
extra | The additional validation data. |
Definition at line 125 of file validatorresult.cpp.
References extra().
Referenced by Cutelyst::Validator::validate().
void ValidatorResult::addValue | ( | const QString & | field, |
const QVariant & | value | ||
) |
field | Name of the input parameter the value has been extracted from. |
value | Value as it has been extracted and maybe converted by the validator. |
Definition at line 110 of file validatorresult.cpp.
References value().
Referenced by Cutelyst::Validator::validate().
QHash< QString, QStringList > ValidatorResult::errors | ( | ) | const |
Definition at line 70 of file validatorresult.cpp.
Referenced by Cutelyst::Validator::validate().
QStringList Cutelyst::ValidatorResult::errors | ( | const QString & | field | ) | const |
field | Name of the field to return error messages for. |
QJsonObject ValidatorResult::errorsJsonObject | ( | ) | const |
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 80 of file validatorresult.cpp.
QStringList ValidatorResult::errorStrings | ( | ) | const |
Definition at line 57 of file validatorresult.cpp.
Referenced by Cutelyst::Validator::validate().
QVariant ValidatorResult::extra | ( | const QString & | field | ) | const |
field | Name of the input parameter to get the extra data for. |
Definition at line 120 of file validatorresult.cpp.
Referenced by addExtra().
QVariantHash ValidatorResult::extras | ( | ) | const |
Definition at line 115 of file validatorresult.cpp.
QStringList ValidatorResult::failedFields | ( | ) | const |
Definition at line 95 of file validatorresult.cpp.
bool ValidatorResult::hasErrors | ( | const QString & | field | ) | const |
field | Name of the input field to check. |
true
if the field has error messages, false
otherwise. Definition at line 75 of file validatorresult.cpp.
bool ValidatorResult::isValid | ( | ) | const |
Definition at line 45 of file validatorresult.cpp.
|
inlineexplicit |
Definition at line 176 of file validatorresult.h.
QVariant ValidatorResult::value | ( | const QString & | field | ) | const |
field | Name of the input parameter to get the value for. |
Definition at line 105 of file validatorresult.cpp.
Referenced by addValue().
QVariantHash ValidatorResult::values | ( | ) | const |
Definition at line 100 of file validatorresult.cpp.