Cutelyst  2.13.0
Public Member Functions | List of all members
Cutelyst::ValidatorResult Class Reference

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...
 
ValidatorResultoperator= (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...
 

Detailed Description

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.

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
}
}

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

Definition at line 81 of file validatorresult.h.

Constructor & Destructor Documentation

◆ ValidatorResult()

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.

Member Function Documentation

◆ addError()

void ValidatorResult::addError ( const QString &  field,
const QString &  message 
)
Parameters
fieldName of the input parameter that has validation errors.
messageError message shown to the user.
See also
errorString() errors() hasErrors()

Definition at line 50 of file validatorresult.cpp.

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

◆ addExtra()

void ValidatorResult::addExtra ( const QString &  field,
const QVariant &  extra 
)
Parameters
fieldName of the input parameter the extra data occured for.
extraThe additional validation data.
See also
extras() extra()
Since
Cutelyst 2.0.0

Definition at line 125 of file validatorresult.cpp.

References extra().

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

◆ addValue()

void ValidatorResult::addValue ( const QString &  field,
const QVariant &  value 
)
Parameters
fieldName of the input parameter the value has been extracted from.
valueValue as it has been extracted and maybe converted by the validator.
See also
values() value()
Since
Cutelyst 2.0.0

Definition at line 110 of file validatorresult.cpp.

References value().

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

◆ errors() [1/2]

QHash< QString, QStringList > ValidatorResult::errors ( ) const
Returns
A QHash containing 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 70 of file validatorresult.cpp.

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

◆ errors() [2/2]

QStringList Cutelyst::ValidatorResult::errors ( const QString &  field) const
Parameters
fieldName of the field to return error messages for.
Returns
List of error message strings. If there were no errors for the field, the returned list will be empty.
See also
errorStrings()

◆ errorsJsonObject()

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.

See also
errors() errorStrings()
Since
Cutelyst 1.12.0

Definition at line 80 of file validatorresult.cpp.

◆ errorStrings()

QStringList ValidatorResult::errorStrings ( ) const
Returns
A list of all error messages from every failed ValidatorRule.

Definition at line 57 of file validatorresult.cpp.

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

◆ extra()

QVariant ValidatorResult::extra ( const QString &  field) const
Parameters
fieldName of the input parameter to get the extra data for.
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 120 of file validatorresult.cpp.

Referenced by addExtra().

◆ extras()

QVariantHash ValidatorResult::extras ( ) const
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 115 of file validatorresult.cpp.

◆ failedFields()

QStringList ValidatorResult::failedFields ( ) const
Returns
A list of of input parameter names that have validation errors.
Since
Cutelyst 1.12.0

Definition at line 95 of file validatorresult.cpp.

◆ hasErrors()

bool ValidatorResult::hasErrors ( const QString &  field) const
Parameters
fieldName of the input field to check.
Returns
true if the field has error messages, false otherwise.
See also
errors()
Since
Cutelyst 2.0.0

Definition at line 75 of file validatorresult.cpp.

◆ isValid()

bool ValidatorResult::isValid ( ) const
Note
A newly constructed ValidatorResult will be valid by default.

Definition at line 45 of file validatorresult.cpp.

◆ operator bool()

Cutelyst::ValidatorResult::operator bool ( ) const
inlineexplicit
Note
A newly constructed ValidatorResult will be valid by default.

Definition at line 176 of file validatorresult.h.

◆ value()

QVariant ValidatorResult::value ( const QString &  field) const
Parameters
fieldName of the input parameter to get the value for.
Returns
The extracted value in 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 105 of file validatorresult.cpp.

Referenced by addValue().

◆ values()

QVariantHash ValidatorResult::values ( ) const
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 100 of file validatorresult.cpp.

Cutelyst::Validator::FillStashOnError
@ FillStashOnError
Definition: validator.h:248
Cutelyst::ValidatorResult::errors
QHash< QString, QStringList > errors() const
Returns a dictionary containing fields with errors.
Definition: validatorresult.cpp:70
Cutelyst::ValidatorResult::ValidatorResult
ValidatorResult()
Constructs a new ValidatorResult.
Definition: validatorresult.cpp:25