Cutelyst
2.13.0
|
Base class for all validator rules. More...
#include <Cutelyst/Plugins/Utils/ValidatorRule>
Public Member Functions | |
ValidatorRule (const QString &field, const ValidatorMessages &messages=ValidatorMessages(), const QString &defValKey=QString()) | |
Constructs a new ValidatorRule with the given parameters. More... | |
virtual | ~ValidatorRule () |
Deconstructs the ValidatorRule. | |
Protected Member Functions | |
ValidatorRule (ValidatorRulePrivate &dd) | |
Constructs a new ValidatorRule object with the given private class. More... | |
void | defaultValue (Context *c, ValidatorReturnType *result, const char *validatorName) const |
I a defValKey has been set in the constructor, this will try to get the default value from the stash and put it into the result. More... | |
QString | field () const |
Returns the name of the field to validate. More... | |
virtual QString | genericParsingError (Context *c, const QVariant &errorData=QVariant()) const |
Returns a generic error message if an error occures while parsing input. More... | |
virtual QString | genericValidationDataError (Context *c, const QVariant &errorData=QVariant()) const |
Returns a generic error message if any validation data is missing or invalid. More... | |
virtual QString | genericValidationError (Context *c, const QVariant &errorData=QVariant()) const |
Returns a generic error mesage if validation failed. More... | |
QString | label (Context *c) const |
Returns the human readable field label used for generic error messages. More... | |
QString | parsingError (Context *c, const QVariant &errorData=QVariant()) const |
Returns an error message if an error occured while parsing input. More... | |
bool | trimBefore () const |
Returns true if the field value should be trimmed before validation. More... | |
virtual ValidatorReturnType | validate (Context *c, const ParamsMultiMap ¶ms) const =0 |
Starts the validation and returns the result. More... | |
QString | validationDataError (Context *c, const QVariant &errorData=QVariant()) const |
Returns an error message if any validation data is missing or invalid. More... | |
QString | validationError (Context *c, const QVariant &errorData=QVariant()) const |
Returns a descriptive error message if validation failed. More... | |
QString | value (const ParamsMultiMap ¶ms) const |
Returns the value of the field from the input params. | |
Friends | |
class | Validator |
class | ValidatorPrivate |
This class can not be used on it's own, you have to create a derived class from it that implements your validator logic. Or use one of the already existing derived validator rules.
If you want to implement your own validator logic to use with Validator, you have to create a class that derives from ValidatorRule. The simplest implementation only needs a constructor and a reimplementation of the validate() function. But for more comfort and usability, you should also reimplement genericValidationError(). If your validator parses the input into a specific type to validate it and/or if you are using additional parameters, you may also want to reimplement genericParsingError() and genericValidationDataError() to return more appropriate generic error messages.
The most important parameter for every validator rule is the name of the field to validate. So your own validator should require that field in the constructor. For better error messages you should also add an optional paramter to set custom ValidatorMessages if validation fails.
In the validation logic implemented in the validate() function you have to return a ValidatorReturnType struct that contains information about the validation. It has three members, errorMessages is the most important one. If that returns true
for QString::isNull(), the validation has succeeded.
So lets implement a custom validator that can check for a specific value to be set. (Maybe not a realistic example, but it should be enough for demonstration.)
That's it. Now you can use your own validator rule in the main Validator.
Definition at line 292 of file validatorrule.h.
ValidatorRule::ValidatorRule | ( | const QString & | field, |
const ValidatorMessages & | messages = ValidatorMessages() , |
||
const QString & | defValKey = QString() |
||
) |
field | Name of the field to validate. |
messages | Custom error messages if validation fails. |
defValKey | Stash key containing a default value if input field is empty. This value will NOT be validated. |
Definition at line 25 of file validatorrule.cpp.
|
protected |
Definition at line 30 of file validatorrule.cpp.
|
protected |
c | Current Context to get the default value from. |
result | The result struct to put the default value in. |
validatorName | Name of the validator used for logging. |
Definition at line 162 of file validatorrule.cpp.
References field(), Cutelyst::Context::stash(), and Cutelyst::ValidatorReturnType::value.
Referenced by Cutelyst::ValidatorFilled::validate(), Cutelyst::ValidatorBoolean::validate(), Cutelyst::ValidatorJson::validate(), Cutelyst::ValidatorIn::validate(), Cutelyst::ValidatorRegularExpression::validate(), Cutelyst::ValidatorInteger::validate(), Cutelyst::ValidatorNotIn::validate(), Cutelyst::ValidatorNumeric::validate(), Cutelyst::ValidatorTime::validate(), Cutelyst::ValidatorDate::validate(), Cutelyst::ValidatorMax::validate(), Cutelyst::ValidatorMin::validate(), Cutelyst::ValidatorDateTime::validate(), Cutelyst::ValidatorSize::validate(), Cutelyst::ValidatorSame::validate(), Cutelyst::ValidatorUrl::validate(), Cutelyst::ValidatorCharNotAllowed::validate(), Cutelyst::ValidatorDigitsBetween::validate(), Cutelyst::ValidatorBetween::validate(), Cutelyst::ValidatorDigits::validate(), Cutelyst::ValidatorAlphaNum::validate(), Cutelyst::ValidatorAlphaDash::validate(), Cutelyst::ValidatorAlpha::validate(), Cutelyst::ValidatorIp::validate(), Cutelyst::ValidatorDomain::validate(), Cutelyst::ValidatorAfter::validate(), Cutelyst::ValidatorBefore::validate(), Cutelyst::ValidatorFileSize::validate(), and Cutelyst::ValidatorEmail::validate().
|
protected |
Definition at line 39 of file validatorrule.cpp.
Referenced by defaultValue(), Cutelyst::ValidatorBetween::genericValidationDataError(), Cutelyst::ValidatorSize::genericValidationDataError(), Cutelyst::ValidatorMax::genericValidationDataError(), Cutelyst::ValidatorMin::genericValidationDataError(), Cutelyst::ValidatorPresent::validate(), Cutelyst::ValidatorFilled::validate(), Cutelyst::ValidatorBoolean::validate(), Cutelyst::ValidatorJson::validate(), Cutelyst::ValidatorConfirmed::validate(), Cutelyst::ValidatorRequiredUnless::validate(), Cutelyst::ValidatorRequiredWithAll::validate(), Cutelyst::ValidatorRequiredWithout::validate(), Cutelyst::ValidatorRequiredWithoutAll::validate(), Cutelyst::ValidatorDifferent::validate(), Cutelyst::ValidatorRequiredIf::validate(), Cutelyst::ValidatorRequiredWith::validate(), Cutelyst::ValidatorRequiredIfStash::validate(), Cutelyst::ValidatorIn::validate(), Cutelyst::ValidatorRegularExpression::validate(), Cutelyst::ValidatorRequiredUnlessStash::validate(), Cutelyst::ValidatorInteger::validate(), Cutelyst::ValidatorAccepted::validate(), Cutelyst::ValidatorNotIn::validate(), Cutelyst::ValidatorTime::validate(), Cutelyst::ValidatorNumeric::validate(), Cutelyst::ValidatorDate::validate(), Cutelyst::ValidatorMax::validate(), Cutelyst::ValidatorMin::validate(), Cutelyst::ValidatorDateTime::validate(), Cutelyst::ValidatorSame::validate(), Cutelyst::ValidatorUrl::validate(), Cutelyst::ValidatorSize::validate(), Cutelyst::ValidatorDigitsBetween::validate(), Cutelyst::ValidatorBetween::validate(), Cutelyst::ValidatorCharNotAllowed::validate(), Cutelyst::ValidatorDigits::validate(), Cutelyst::ValidatorAlphaNum::validate(), Cutelyst::ValidatorAlphaDash::validate(), Cutelyst::ValidatorAlpha::validate(), Cutelyst::ValidatorIp::validate(), Cutelyst::ValidatorBefore::validate(), Cutelyst::ValidatorAfter::validate(), Cutelyst::ValidatorPwQuality::validate(), and Cutelyst::Validator::validate().
|
protectedvirtual |
If you want to have a more specific generic parsing error message for your validator if parsing of input data failes, reimplement this in your derived class. The default implementation simply returns a maybe translated version of "The input data in the “%1“ field could not be parsed."
if there has been a label set or "The input data could not be parsed."
if the label is empty.
The pointer to the current Context c can be used to translate error strings. If you have some more data to use for the error messages, put them into errorData.
Reimplemented in Cutelyst::ValidatorAfter, Cutelyst::ValidatorBefore, Cutelyst::ValidatorBetween, Cutelyst::ValidatorMax, Cutelyst::ValidatorMin, and Cutelyst::ValidatorSize.
Definition at line 119 of file validatorrule.cpp.
References label(), and Cutelyst::Context::translate().
Referenced by parsingError().
|
protectedvirtual |
If you want to have a more specific generic validation data error message for your validator if data needed for the validation is missing or invalid, reimplement this in your derived class. The default implementation simply returns a maybe translated version of "Missing or invalid validation data for the “%1” field."
if there has been a label set or "Missing or invalid validation data."
if the label is empty.
The pointer to the current Context c can be used to translate error strings. If you have some more data to use for the error messages, put them into errorData.
Reimplemented in Cutelyst::ValidatorAfter, Cutelyst::ValidatorBefore, Cutelyst::ValidatorCharNotAllowed, Cutelyst::ValidatorFileSize, Cutelyst::ValidatorBetween, Cutelyst::ValidatorMax, Cutelyst::ValidatorMin, Cutelyst::ValidatorSize, Cutelyst::ValidatorNotIn, and Cutelyst::ValidatorIn.
Definition at line 149 of file validatorrule.cpp.
References label(), and Cutelyst::Context::translate().
Referenced by validationDataError().
|
protectedvirtual |
If you want to have a more specifc generic validation error message for your validator if validation fails, reimplment this your derived class. The default implementation simply returns a maybe translated version of "The input data in the “%1” field is not acceptable."
if there has been a label set or "The input data is not acceptable."
if the label is empty.
The pointer to the current Context c can be used to translate error strings. If you have some more data to use for the error messages, put them into errorData.
Reimplemented in Cutelyst::ValidatorEmail, Cutelyst::ValidatorFileSize, Cutelyst::ValidatorAfter, Cutelyst::ValidatorBefore, Cutelyst::ValidatorDomain, Cutelyst::ValidatorIp, Cutelyst::ValidatorAlpha, Cutelyst::ValidatorAlphaDash, Cutelyst::ValidatorAlphaNum, Cutelyst::ValidatorDigits, Cutelyst::ValidatorDigitsBetween, Cutelyst::ValidatorBetween, Cutelyst::ValidatorCharNotAllowed, Cutelyst::ValidatorMax, Cutelyst::ValidatorMin, Cutelyst::ValidatorSame, Cutelyst::ValidatorSize, Cutelyst::ValidatorUrl, Cutelyst::ValidatorDateTime, Cutelyst::ValidatorDate, Cutelyst::ValidatorNumeric, Cutelyst::ValidatorTime, Cutelyst::ValidatorAccepted, Cutelyst::ValidatorInteger, Cutelyst::ValidatorNotIn, Cutelyst::ValidatorRequiredUnlessStash, Cutelyst::ValidatorIn, Cutelyst::ValidatorRegularExpression, Cutelyst::ValidatorRequiredIfStash, Cutelyst::ValidatorRequiredIf, Cutelyst::ValidatorRequiredWith, Cutelyst::ValidatorDifferent, Cutelyst::ValidatorJson, Cutelyst::ValidatorRequiredUnless, Cutelyst::ValidatorRequiredWithAll, Cutelyst::ValidatorRequiredWithout, Cutelyst::ValidatorRequiredWithoutAll, Cutelyst::ValidatorConfirmed, Cutelyst::ValidatorBoolean, Cutelyst::ValidatorRequired, Cutelyst::ValidatorFilled, Cutelyst::ValidatorPresent, and Cutelyst::ValidatorPwQuality.
Definition at line 89 of file validatorrule.cpp.
References label(), and Cutelyst::Context::translate().
Referenced by validationError().
|
protected |
The label can be set in the ValidatorMessages on the constructor.
Definition at line 58 of file validatorrule.cpp.
References Cutelyst::Context::translate().
Referenced by Cutelyst::ValidatorEmail::categoryString(), Cutelyst::ValidatorDomain::diagnoseString(), Cutelyst::ValidatorEmail::diagnoseString(), Cutelyst::ValidatorPwQuality::errorString(), Cutelyst::ValidatorMax::genericParsingError(), Cutelyst::ValidatorMin::genericParsingError(), Cutelyst::ValidatorBetween::genericParsingError(), Cutelyst::ValidatorSize::genericParsingError(), Cutelyst::ValidatorBefore::genericParsingError(), Cutelyst::ValidatorAfter::genericParsingError(), genericParsingError(), Cutelyst::ValidatorIn::genericValidationDataError(), Cutelyst::ValidatorNotIn::genericValidationDataError(), Cutelyst::ValidatorMax::genericValidationDataError(), Cutelyst::ValidatorMin::genericValidationDataError(), Cutelyst::ValidatorBetween::genericValidationDataError(), Cutelyst::ValidatorCharNotAllowed::genericValidationDataError(), Cutelyst::ValidatorSize::genericValidationDataError(), Cutelyst::ValidatorFileSize::genericValidationDataError(), genericValidationDataError(), Cutelyst::ValidatorPresent::genericValidationError(), Cutelyst::ValidatorFilled::genericValidationError(), Cutelyst::ValidatorRequired::genericValidationError(), Cutelyst::ValidatorBoolean::genericValidationError(), Cutelyst::ValidatorConfirmed::genericValidationError(), Cutelyst::ValidatorDifferent::genericValidationError(), Cutelyst::ValidatorJson::genericValidationError(), Cutelyst::ValidatorRequiredUnless::genericValidationError(), Cutelyst::ValidatorRequiredWithAll::genericValidationError(), Cutelyst::ValidatorRequiredWithout::genericValidationError(), Cutelyst::ValidatorRequiredWithoutAll::genericValidationError(), Cutelyst::ValidatorRequiredIf::genericValidationError(), Cutelyst::ValidatorRequiredWith::genericValidationError(), Cutelyst::ValidatorRegularExpression::genericValidationError(), Cutelyst::ValidatorRequiredIfStash::genericValidationError(), Cutelyst::ValidatorIn::genericValidationError(), Cutelyst::ValidatorRequiredUnlessStash::genericValidationError(), Cutelyst::ValidatorNotIn::genericValidationError(), Cutelyst::ValidatorAccepted::genericValidationError(), Cutelyst::ValidatorInteger::genericValidationError(), Cutelyst::ValidatorTime::genericValidationError(), Cutelyst::ValidatorNumeric::genericValidationError(), Cutelyst::ValidatorDate::genericValidationError(), Cutelyst::ValidatorDateTime::genericValidationError(), Cutelyst::ValidatorSize::genericValidationError(), Cutelyst::ValidatorMax::genericValidationError(), Cutelyst::ValidatorMin::genericValidationError(), Cutelyst::ValidatorUrl::genericValidationError(), Cutelyst::ValidatorSame::genericValidationError(), Cutelyst::ValidatorCharNotAllowed::genericValidationError(), Cutelyst::ValidatorBetween::genericValidationError(), Cutelyst::ValidatorDigitsBetween::genericValidationError(), Cutelyst::ValidatorDigits::genericValidationError(), Cutelyst::ValidatorAlphaNum::genericValidationError(), Cutelyst::ValidatorAlphaDash::genericValidationError(), Cutelyst::ValidatorAlpha::genericValidationError(), Cutelyst::ValidatorIp::genericValidationError(), Cutelyst::ValidatorDomain::genericValidationError(), Cutelyst::ValidatorAfter::genericValidationError(), Cutelyst::ValidatorBefore::genericValidationError(), Cutelyst::ValidatorPwQuality::genericValidationError(), Cutelyst::ValidatorFileSize::genericValidationError(), Cutelyst::ValidatorEmail::genericValidationError(), and genericValidationError().
|
protected |
This will either return the customParsingError message provided via the ValidatorMessages in the messages argument of the constructor or the message returned by genericValidationError() if there is no customParsingError message availabe.
When writing a new ValidatorRule, use this in your reimplementation of validate() if parsing of input data fails.
The pointer to the current Context c will be used to translate error strings. If you have some more data to use for the error messages, put them into errorData.
Definition at line 102 of file validatorrule.cpp.
References genericParsingError(), and Cutelyst::Context::translate().
Referenced by Cutelyst::ValidatorMax::validate(), Cutelyst::ValidatorMin::validate(), Cutelyst::ValidatorSize::validate(), Cutelyst::ValidatorBetween::validate(), Cutelyst::ValidatorAfter::validate(), and Cutelyst::ValidatorBefore::validate().
|
protected |
By default, this will return true
and all input values will be trimmed before validation to remove whitespaces from the beginning and the end.
Definition at line 179 of file validatorrule.cpp.
Referenced by Cutelyst::ValidatorConfirmed::validate(), Cutelyst::ValidatorDifferent::validate(), Cutelyst::ValidatorRequiredUnless::validate(), Cutelyst::ValidatorRequiredIf::validate(), and Cutelyst::ValidatorSame::validate().
|
protectedpure virtual |
This is the main function to reimplement when writing a custom validator. When reimplementing this function in a class derived from ValidatorRule, you have to return an empty errorMessage if validation succeeded and the corresponding error if it fails. There are currently three error functions that should be used for different error cases:
If validation succeeded, you should put the extracted and validated value into the ValidatorReturnType::value. After the validation you can get the extracted values from ValidatorResult::values().
Implemented in Cutelyst::ValidatorEmail, Cutelyst::ValidatorFileSize, Cutelyst::ValidatorPwQuality, Cutelyst::ValidatorAfter, Cutelyst::ValidatorBefore, Cutelyst::ValidatorDomain, Cutelyst::ValidatorIp, Cutelyst::ValidatorAlpha, Cutelyst::ValidatorAlphaDash, Cutelyst::ValidatorAlphaNum, Cutelyst::ValidatorDigits, Cutelyst::ValidatorBetween, Cutelyst::ValidatorCharNotAllowed, Cutelyst::ValidatorDigitsBetween, Cutelyst::ValidatorSame, Cutelyst::ValidatorSize, Cutelyst::ValidatorUrl, Cutelyst::ValidatorDateTime, Cutelyst::ValidatorMax, Cutelyst::ValidatorMin, Cutelyst::ValidatorDate, Cutelyst::ValidatorNumeric, Cutelyst::ValidatorTime, Cutelyst::ValidatorAccepted, Cutelyst::ValidatorInteger, Cutelyst::ValidatorNotIn, Cutelyst::ValidatorRequiredUnlessStash, Cutelyst::ValidatorIn, Cutelyst::ValidatorRegularExpression, Cutelyst::ValidatorRequiredIfStash, Cutelyst::ValidatorRequiredIf, Cutelyst::ValidatorRequiredWith, Cutelyst::ValidatorDifferent, Cutelyst::ValidatorRequiredUnless, Cutelyst::ValidatorRequiredWithAll, Cutelyst::ValidatorRequiredWithout, Cutelyst::ValidatorRequiredWithoutAll, Cutelyst::ValidatorConfirmed, Cutelyst::ValidatorBoolean, Cutelyst::ValidatorJson, Cutelyst::ValidatorRequired, Cutelyst::ValidatorFilled, and Cutelyst::ValidatorPresent.
Referenced by Cutelyst::Validator::validate().
|
protected |
This will either return the customValidationDataError message provided via the ValidatorMessages in the messages argument of the contstructor or the message returned by genericValidationDataError() if there is no customValidationDataError message available.
When writing a new ValidatorRule, use this in your reimplementation of validate() if validation data like compare values are missing or invalid.
The pointer to the current Context c will be used to translate error strings. If you have some more data to use for the error messages, put them into errorData.
Definition at line 132 of file validatorrule.cpp.
References genericValidationDataError(), and Cutelyst::Context::translate().
Referenced by Cutelyst::ValidatorAfter::genericParsingError(), Cutelyst::ValidatorBefore::genericParsingError(), Cutelyst::ValidatorMax::genericValidationError(), Cutelyst::ValidatorMin::genericValidationError(), Cutelyst::ValidatorSize::genericValidationError(), Cutelyst::ValidatorBetween::genericValidationError(), Cutelyst::ValidatorBefore::genericValidationError(), Cutelyst::ValidatorAfter::genericValidationError(), Cutelyst::ValidatorRequiredUnless::validate(), Cutelyst::ValidatorRequiredWithout::validate(), Cutelyst::ValidatorRequiredWithAll::validate(), Cutelyst::ValidatorRequiredWithoutAll::validate(), Cutelyst::ValidatorRequiredIf::validate(), Cutelyst::ValidatorRequiredWith::validate(), Cutelyst::ValidatorIn::validate(), Cutelyst::ValidatorRegularExpression::validate(), Cutelyst::ValidatorRequiredIfStash::validate(), Cutelyst::ValidatorNotIn::validate(), Cutelyst::ValidatorRequiredUnlessStash::validate(), Cutelyst::ValidatorInteger::validate(), Cutelyst::ValidatorMin::validate(), Cutelyst::ValidatorMax::validate(), Cutelyst::ValidatorSize::validate(), Cutelyst::ValidatorBetween::validate(), Cutelyst::ValidatorCharNotAllowed::validate(), Cutelyst::ValidatorDigitsBetween::validate(), Cutelyst::ValidatorDigits::validate(), Cutelyst::ValidatorBefore::validate(), Cutelyst::ValidatorAfter::validate(), and Cutelyst::ValidatorFileSize::validate().
|
protected |
This will either return the customValidationError message provided via the ValidatorMessages in the messages argument of the constructor or the message returned by genericValidationError() if there is no customValidationError message availabe.
When writing a new ValidatorRule, use this in your reimplementaion of validate() if validation failed.
The pointer to the current Context c will be used to translate error strings. If you have some more data to use for the error messages, put them into errorData.
Definition at line 72 of file validatorrule.cpp.
References genericValidationError(), and Cutelyst::Context::translate().
Referenced by Cutelyst::ValidatorPresent::validate(), Cutelyst::ValidatorFilled::validate(), Cutelyst::ValidatorRequired::validate(), Cutelyst::ValidatorBoolean::validate(), Cutelyst::ValidatorJson::validate(), Cutelyst::ValidatorConfirmed::validate(), Cutelyst::ValidatorRequiredWithoutAll::validate(), Cutelyst::ValidatorRequiredUnless::validate(), Cutelyst::ValidatorRequiredWithAll::validate(), Cutelyst::ValidatorRequiredWithout::validate(), Cutelyst::ValidatorDifferent::validate(), Cutelyst::ValidatorRequiredIf::validate(), Cutelyst::ValidatorRequiredWith::validate(), Cutelyst::ValidatorRequiredIfStash::validate(), Cutelyst::ValidatorIn::validate(), Cutelyst::ValidatorRegularExpression::validate(), Cutelyst::ValidatorRequiredUnlessStash::validate(), Cutelyst::ValidatorAccepted::validate(), Cutelyst::ValidatorInteger::validate(), Cutelyst::ValidatorNotIn::validate(), Cutelyst::ValidatorNumeric::validate(), Cutelyst::ValidatorTime::validate(), Cutelyst::ValidatorDate::validate(), Cutelyst::ValidatorMin::validate(), Cutelyst::ValidatorMax::validate(), Cutelyst::ValidatorDateTime::validate(), Cutelyst::ValidatorSame::validate(), Cutelyst::ValidatorSize::validate(), Cutelyst::ValidatorUrl::validate(), Cutelyst::ValidatorCharNotAllowed::validate(), Cutelyst::ValidatorBetween::validate(), Cutelyst::ValidatorDigitsBetween::validate(), Cutelyst::ValidatorDigits::validate(), Cutelyst::ValidatorAlphaNum::validate(), Cutelyst::ValidatorAlphaDash::validate(), Cutelyst::ValidatorAlpha::validate(), Cutelyst::ValidatorIp::validate(), Cutelyst::ValidatorDomain::validate(), Cutelyst::ValidatorAfter::validate(), Cutelyst::ValidatorBefore::validate(), Cutelyst::ValidatorPwQuality::validate(), Cutelyst::ValidatorFileSize::validate(), and Cutelyst::ValidatorEmail::validate().