Cutelyst  2.5.0
Public Member Functions | Public Attributes | List of all members
Cutelyst::ValidatorMessages Class Reference

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

#include <Cutelyst/Plugins/Utils/ValidatorRule>

Public Member Functions

 ValidatorMessages ()
 Constructs a default ValidatorMessages object with all custom messages disabled.
 
 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() so that the message can be dynamically translated by the current Context . 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.

The translation context used in the QT_TRANSLATE_NOOP() definition has to be the same that has been set on the Validator contstructor. If you dont want to use translation for the messages and the label, simply don't use QT_TRANSLATE_NOOP() when adding the string but simply use a C string literal and also leave the translation context on the Validator constructor empty.

Usage example

void MyController::do_form(Context *c)
{
static Validator v1({
new ValidatorAccepted(QStringLiteral("usage_terms"),
// empty label and translatable validation error message
ValidatorMessages(nullptr, 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", "You 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()
}, QLatin1String("MyController"));
// this validator does not specify a translation context and will therefore not translate error messages,
// even if they were added with QT_TRANSLATE_NOOP()
static Validator v2({
new ValidatorRequired(QStringLiteral("required_field"),
ValidatorMessages("Required Field", "This field is required, please enter data."))
});
}

Definition at line 144 of file validatorrule.h.

Constructor & Destructor Documentation

◆ ValidatorMessages()

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 157 of file validatorrule.h.

Member Data Documentation

◆ label

const char* Cutelyst::ValidatorMessages::label

Field label used for generating generic error messages.

Definition at line 163 of file validatorrule.h.

◆ parsingError

const char* Cutelyst::ValidatorMessages::parsingError

Custom parsing error message.

Definition at line 165 of file validatorrule.h.

◆ validationDataError

const char* Cutelyst::ValidatorMessages::validationDataError

Custom validation data error message.

Definition at line 166 of file validatorrule.h.

◆ validationError

const char* Cutelyst::ValidatorMessages::validationError

Custom validation error messages.

Definition at line 164 of file validatorrule.h.