-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validator plugin: add new ValidatorCharNotAllowed
Simple validator that checks if the input value contains a character from a list of forbidden characters.
- Loading branch information
1 parent
270bc10
commit 4eb2e71
Showing
9 changed files
with
303 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Cutelyst/Plugins/Utils/Validator/validatorcharnotallowed.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (C) 2019 Matthias Fehring <mf@huessenbergnetz.de> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#include "validatorcharnotallowed_p.h" | ||
|
||
using namespace Cutelyst; | ||
|
||
ValidatorCharNotAllowed::ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages, const QString &defValKey) : | ||
ValidatorRule(*new ValidatorCharNotAllowedPrivate(field, forbiddenChars, messages, defValKey)) | ||
{ | ||
|
||
} | ||
|
||
ValidatorCharNotAllowed::~ValidatorCharNotAllowed() | ||
{ | ||
|
||
} | ||
|
||
bool ValidatorCharNotAllowed::validate(const QString &value, const QString &forbiddenChars, QChar *foundChar) | ||
{ | ||
bool valid = true; | ||
|
||
for (const QChar &forbiddenChar : forbiddenChars) { | ||
if (value.contains(forbiddenChar)) { | ||
valid = false; | ||
if (foundChar) { | ||
*foundChar = forbiddenChar; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
return valid; | ||
} | ||
|
||
ValidatorReturnType ValidatorCharNotAllowed::validate(Context *c, const ParamsMultiMap ¶ms) const | ||
{ | ||
ValidatorReturnType result; | ||
|
||
Q_D(const ValidatorCharNotAllowed); | ||
|
||
const QString v = value(params); | ||
if (!v.isEmpty()) { | ||
if (Q_LIKELY(!d->forbiddenChars.isEmpty())) { | ||
QChar foundChar; | ||
if (Q_LIKELY(ValidatorCharNotAllowed::validate(v, d->forbiddenChars, &foundChar))) { | ||
result.value.setValue<QString>(v); | ||
} else { | ||
result.errorMessage = validationError(c, foundChar); | ||
} | ||
} else { | ||
qCWarning(C_VALIDATOR) << "ValidatorCharNotAllowed: Empty validation data for field" << field() << "at" << c->controllerName() << "::" << c->actionName(); | ||
result.errorMessage = validationDataError(c); | ||
} | ||
} else { | ||
defaultValue(c, &result, "ValidatorCharNotAllowed"); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
QString ValidatorCharNotAllowed::genericValidationError(Context *c, const QVariant &errorData) const | ||
{ | ||
QString error; | ||
const QChar foundChar = errorData.toChar(); | ||
Q_D(const ValidatorCharNotAllowed); | ||
const QString _label = label(c); | ||
if (_label.isEmpty()) { | ||
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "Must not contain the following characters: “%1”. But contains the following illegal character: “%2”.").arg(d->forbiddenChars, QString(foundChar)); | ||
} else { | ||
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The text in the “%1“ field must not contain the following characters: “%2“. But contains the following illegal character: “%3”.").arg(_label, d->forbiddenChars, QString(foundChar)); | ||
} | ||
|
||
return error; | ||
} | ||
|
||
QString ValidatorCharNotAllowed::genericValidationDataError(Context *c, const QVariant &errorData) const | ||
{ | ||
QString error; | ||
Q_UNUSED(errorData) | ||
const QString _label = label(c); | ||
if (_label.isEmpty()) { | ||
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for this field is empty."); | ||
} else { | ||
error = c->translate("Cutelyst::ValidatorCharNotAllowed", "The list of illegal characters for the “%1“ field is empty.").arg(_label); | ||
} | ||
return error; | ||
} |
101 changes: 101 additions & 0 deletions
101
Cutelyst/Plugins/Utils/Validator/validatorcharnotallowed.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright (C) 2019 Matthias Fehring <mf@huessenbergnetz.de> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef CUTELYSTVALIDATORCHARNOTALLOWED_H | ||
#define CUTELYSTVALIDATORCHARNOTALLOWED_H | ||
|
||
#include <Cutelyst/cutelyst_global.h> | ||
#include "validatorrule.h" | ||
|
||
namespace Cutelyst { | ||
|
||
class ValidatorCharNotAllowedPrivate; | ||
|
||
/*! | ||
* \ingroup plugins-utils-validator-rules | ||
* \class ValidatorCharNotAllowed validatorcharnotallowed.h <Cutelyst/Plugins/Utils/validatorcharnotallowed.h> | ||
* \brief Validates an input field for not allowed characters. | ||
* | ||
* The \a field under validation is not allowed to contain a list of characters. | ||
* The list of not allowed characters is set as a QString to the \a forbiddenChars. | ||
* | ||
* \note Unless \link Validator::validate() validation\endlink is started with \link Validator::NoTrimming NoTrimming\endlink, | ||
* whitespaces will be removed from the beginning and the end of the input value before validation. | ||
* If the \a field's value is empty or if the \a field is missing in the input data, the validation will succeed without | ||
* performing the validation itself. Use one of the \link ValidatorRequired required validators \endlink to require the | ||
* field to be present and not empty. | ||
* | ||
* \sa Validator for general usage of validators. | ||
* | ||
* \sa ValidatorRegularExpression | ||
*/ | ||
class CUTELYST_PLUGIN_UTILS_VALIDATOR_EXPORT ValidatorCharNotAllowed : public ValidatorRule | ||
{ | ||
public: | ||
/*! | ||
* \brief Constructs a new char not allowed validator. | ||
* | ||
* \param field Name of the input field to validate. | ||
* \param forbiddenChars List of characters not allowed in the input field. | ||
* \param messages Custom error messages if validation fails. | ||
* \param defValKey \link Context::stash() Stash \endlink key containing a default value if input field is empty. This value will \b NOT be validated. | ||
*/ | ||
ValidatorCharNotAllowed(const QString &field, const QString &forbiddenChars, const ValidatorMessages &messages = ValidatorMessages(), const QString &defValKey = QString()); | ||
|
||
/*! | ||
* \brief Deconstructs the char not allowed validator. | ||
*/ | ||
~ValidatorCharNotAllowed() override; | ||
|
||
/*! | ||
* \ingroup plugins-utils-validator-rules | ||
* \brief Returns \c true if \a value does not contain any of the charachters in \a forbiddenChars | ||
* \param value The value to validate. | ||
* \param forbiddenChars The list of forbidden characters. | ||
* \param foundChar If set, it will contain the first found chararacter that is forbidden. | ||
* \return \c true if the \a value does not contain any of the \a forbiddenChars | ||
*/ | ||
static bool validate(const QString &value, const QString &forbiddenChars, QChar *foundChar = nullptr); | ||
|
||
protected: | ||
/*! | ||
* \brief Performs the validation and returns the result. | ||
* | ||
* If validation succeeded, ValidatorReturnType::value will contain the input paramter | ||
* value as QString. | ||
*/ | ||
ValidatorReturnType validate(Context *c, const ParamsMultiMap ¶ms) const override; | ||
|
||
/*! | ||
* \brief Returns a generic error message if validation failed. | ||
*/ | ||
QString genericValidationError(Context *c, const QVariant &errorData = QVariant()) const override; | ||
|
||
/*! | ||
* \brief Returns a generic error if the list of forbidden characters is empty. | ||
*/ | ||
QString genericValidationDataError(Context *c, const QVariant &errorData = QVariant()) const override; | ||
|
||
private: | ||
Q_DECLARE_PRIVATE(ValidatorCharNotAllowed) | ||
Q_DISABLE_COPY(ValidatorCharNotAllowed) | ||
}; | ||
|
||
} | ||
|
||
#endif // CUTELYSTVALIDATORCHARNOTALLOWED_H |
40 changes: 40 additions & 0 deletions
40
Cutelyst/Plugins/Utils/Validator/validatorcharnotallowed_p.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (C) 2019 Matthias Fehring <mf@huessenbergnetz.de> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
|
||
#ifndef CUTELYSTVALIDATORCHARNOTALLOWED_P_H | ||
#define CUTELYSTVALIDATORCHARNOTALLOWED_P_H | ||
|
||
#include "validatorcharnotallowed.h" | ||
#include "validatorrule_p.h" | ||
|
||
namespace Cutelyst { | ||
|
||
class ValidatorCharNotAllowedPrivate : public ValidatorRulePrivate | ||
{ | ||
public: | ||
ValidatorCharNotAllowedPrivate(const QString &f, const QString &fcs, const ValidatorMessages &m, const QString &dvk) : | ||
ValidatorRulePrivate(f, m, dvk), | ||
forbiddenChars(fcs) | ||
{} | ||
|
||
QString forbiddenChars; | ||
}; | ||
|
||
} | ||
|
||
#endif // CUTELYSTVALIDATORCHARNOTALLOWED_P_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters