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

Contains the result of Validator. More...

#include <validatorresult.h>

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. More...
QHash< QString, QStringList > errors () const
 Returns a dictionary containing fields with errors. More...
QStringList errorStrings () const
 Returns a list of error messages. 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 object.

Detailed Description

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

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

static Validator v(c, {new ValidatorRequired(QStringLiteral("required_field")});
ValidatorResult r = v.validate();
if (r) {
// do stuff if validation was successful
} else {
// do other stuff if validation failed
// 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 62 of file validatorresult.h.

Constructor & Destructor Documentation

Cutelyst::ValidatorResult::ValidatorResult ( )

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

Member Function Documentation

void Cutelyst::ValidatorResult::addError ( const QString &  field,
const QString &  message 
fieldName of the input field that has input errors.
messageError message shown to the user.
QHash<QString,QStringList> Cutelyst::ValidatorResult::errors ( ) const

Returns a dictionary that contains the fields that have validation errors together with the errors strings. The key will be the field name, the value will be the list of errors for that field.

QStringList Cutelyst::ValidatorResult::errorStrings ( ) const

Returns a list of all error messages from every failed ValidatorRule.

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

Definition at line 122 of file validatorresult.h.