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

Stores custom error messages and the input field label. More...

#include <Cutelyst/Plugins/Utils/ValidatorRule>

Public Member Functions

 ValidatorMessages ()=default
 
 ValidatorMessages (const char *customLabel, const char *customValidationError=nullptr, const char *customParsingError=nullptr, const char *customValidationDataError=nullptr)
 Constructs a new ValidatorMessages object with the given parameters. More...
 

Public Attributes

const char * label
 
const char * parsingError
 
const char * validationDataError
 
const char * validationError
 

Detailed Description

This struct is used by ValidatorRule derived classes to store custom error messages that are also translatable. To make the messages translatable, use QT_TRANSLATE_NOOP() or QT_TRID_NOOP() so that the message can be dynamically translated by the current Context either via Context::translate() or Context::qtTrId(). If you want to omit a custom message, simply use a nullptr for it. For custom messages that are not set, the ValidatorRule class will return a generic message that will also be translated if Cutelyst has a translation file for the current language in the context.

Note that you should not mix id based and non-id based translations, so you should not mix QT_TRID_NOOP and QT_TRANSLATE_NOOP.

If using QT_TRANSLATE_NOOP you have to set the translation context to the constructor of Validator. Id based translations do not use a translation context. So, if you omit the translation context on the constructor of Validator, internally Context::qtTrId() will be used to find the translated string.

See also
Translate your application

Usage example

void MyController::do_form(Context *c)
{
static Validator v1({
new ValidatorAccepted(QStringLiteral("usage_terms"),
// empty label and translatable validation error message
QT_TRANSLATE_NOOP("MyController",
"Please accept our terms of "
"usage to finish your "
"registration.")
),
new ValidatorEmail(QStringLiteral("email"), false, ValidatorEmail::Valid,
// only the label will be translated and inserted into generic error
// messages of the validator rule
ValidatorMessages(QT_TRANSLATE_NOOP("MyController",
"Your Email Address"))
),
new ValidatorDate(QStringLiteral("birthday"),
// here we also use a translatable version of the date format
QT_TRANSLATE_NOOP("MyControler", "yyyy-MM-dd"),
// this one will use a translated label for generic parsing and validation
// data error messages and a custom translatable error message for failed
// validations
ValidatorMessages(QT_TRANSLATE_NOOP("MyController",
"Your day of birth"),
QT_TRANSLATE_NOOP("MyController",
"Please enter a valid date of birth"))
),
// this uses a default constructed ValidatorMessages struct where no custom messages
// have been set, so all error messages will come from the generic validator messages
// without a label
new ValidatorRequired(QStringLiteral("password"));
// this is the context that will be used to obtain translations,
// it has to be the same one you use in your validator messages
// QT_TRANSLATE_NOOP(). If you used QT_TRID_NOOP() you can omit
// this
}, "MyController");
}

Definition at line 142 of file validatorrule.h.

Constructor & Destructor Documentation

◆ ValidatorMessages() [1/2]

Cutelyst::ValidatorMessages::ValidatorMessages ( )
default

Constructs a default ValidatorMessages object with all custom messages disabled.

◆ ValidatorMessages() [2/2]

Cutelyst::ValidatorMessages::ValidatorMessages ( const char *  customLabel,
const char *  customValidationError = nullptr,
const char *  customParsingError = nullptr,
const char *  customValidationDataError = nullptr 
)
inline
Parameters
customLabelUser visible label for the input field. Should be the same as used on the frontend visible to the user. Will be used by generic error messages if set.
customValidationErrorCustom error message if the validation fails.
customParsingErrorCustom error message if the input value could not be parsed.
customValidationDataErrorCustom error message if validation data is missing or invalid.

Definition at line 158 of file validatorrule.h.

Member Data Documentation

◆ label

const char* Cutelyst::ValidatorMessages::label

Field label used for generating generic error messages.

Definition at line 168 of file validatorrule.h.

◆ parsingError

const char* Cutelyst::ValidatorMessages::parsingError

Custom parsing error message.

Definition at line 170 of file validatorrule.h.

◆ validationDataError

const char* Cutelyst::ValidatorMessages::validationDataError

Custom validation data error message.

Definition at line 171 of file validatorrule.h.

◆ validationError

const char* Cutelyst::ValidatorMessages::validationError

Custom validation error messages.

Definition at line 169 of file validatorrule.h.