Cutelyst  1.11.0
validatorregularexpression.cpp
1 /*
2  * Copyright (C) 2017 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "validatorregularexpression_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorRegularExpression::ValidatorRegularExpression(const QString &field, const QRegularExpression &regex, const QString &label, const QString &customError) :
24  ValidatorRule(*new ValidatorRegularExpressionPrivate(field, regex, label, customError))
25 {
26 }
27 
28 ValidatorRegularExpression::ValidatorRegularExpression(ValidatorRegularExpressionPrivate &dd) :
29  ValidatorRule(dd)
30 {
31 }
32 
34 {
35 }
36 
38 {
39  QString result;
40 
41  Q_D(const ValidatorRegularExpression);
42 
43  if (!value().isEmpty() && !value().contains(d->regex)) {
44  result = validationError();
45  }
46 
47  return result;
48 }
49 
51 {
52  QString error;
53  if (label().isEmpty()) {
54  error = QStringLiteral("Does not match desired format.");
55  } else {
56  error = QStringLiteral("The “%1” field does not match the desired format.").arg(label());
57  }
58  return error;
59 }
60 
61 void ValidatorRegularExpression::setRegex(const QRegularExpression &regex)
62 {
64  d->regex = regex;
65 }
QString value() const
Returns the field value.
void setRegex(const QRegularExpression &regex)
Sets the regular expression to check.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
QString validationError() const
Returns a descriptive error message if validation failed.
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Base class for all validators.
QString label() const
Returns the human readable field label used for generic error messages.
QString genericValidationError() const override
Returns a generic error message.
~ValidatorRegularExpression()
Deconstructs the regex validator.
The field under validation must match the given regular expression.
ValidatorRegularExpression(const QString &field, const QRegularExpression &regex, const QString &label=QString(), const QString &customError=QString())
Constructs a new regex validator.