Cutelyst  1.11.0
validatorrule.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 "validatorrule_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorRule::ValidatorRule(const QString &field, const QString &label, const QString &customError) :
24  d_ptr(new ValidatorRulePrivate(field, label, customError))
25 {
26 }
27 
28 ValidatorRule::ValidatorRule(ValidatorRulePrivate &dd) :
29  d_ptr(&dd)
30 {
31 }
32 
34 {
35 }
36 
37 QString ValidatorRule::field() const { Q_D(const ValidatorRule); return d->field; }
38 
39 void ValidatorRule::setField(const QString &field)
40 {
41  Q_D(ValidatorRule);
42  d->field = field;
43 }
44 
45 QString ValidatorRule::label() const { Q_D(const ValidatorRule); return d->label; }
46 
47 void ValidatorRule::setLabel(const QString &label)
48 {
49  Q_D(ValidatorRule);
50  d->label = label;
51 }
52 
53 QString ValidatorRule::value() const
54 {
55  QString v;
56 
57  Q_D(const ValidatorRule);
58 
59  if (!field().isEmpty() && !d->parameters.isEmpty()) {
60  if (trimBefore()) {
61  v = d->parameters.value(field()).trimmed();
62  } else {
63  v = d->parameters.value(field());
64  }
65  }
66 
67  return v;
68 }
69 
70 ParamsMultiMap ValidatorRule::parameters() const { Q_D(const ValidatorRule); return d->parameters; }
71 
73 {
74  Q_D(ValidatorRule);
75  d->parameters = params;
76 }
77 
79 {
80  QString error;
81  Q_D(const ValidatorRule);
82  if (d->customError.isEmpty()) {
83  error = genericValidationError();
84  } else {
85  error = d->customError;
86  }
87  return error;
88 }
89 
91 {
92  QString error;
93  if (label().isEmpty()) {
94  error = QStringLiteral("Input value is not acceptable.");
95  } else {
96  error = QStringLiteral("The input data in the “%1” field is not valid.").arg(label());
97  }
98  return error;
99 }
100 
102 {
103  QString error;
104  Q_D(const ValidatorRule);
105  if (d->customParsingError.isEmpty()) {
106  error = genericParsingError();
107  } else {
108  error = d->customParsingError;
109  }
110  return error;
111 }
112 
114 {
115  QString error;
116  if (label().isEmpty()) {
117  error = QStringLiteral("Failed to parse input data.");
118  } else {
119  error = QStringLiteral("Failed to parse the input data of the “%1” field.").arg(label());
120  }
121  return error;
122 }
123 
125 {
126  QString error;
127  Q_D(const ValidatorRule);
128  if (d->customValidationDataError.isEmpty()) {
129  error = genericValidationDataError();
130  } else {
131  error = d->customValidationDataError;
132  }
133  return error;
134 }
135 
137 {
138  QString error;
139  if (label().isEmpty()) {
140  error = QStringLiteral("Missing or unusable validation data.");
141  } else {
142  error = QStringLiteral("Missing or unusable validation data for the “%1” field.").arg(label());
143  }
144  return error;
145 }
146 
147 void ValidatorRule::setCustomError(const QString &customError)
148 {
149  Q_D(ValidatorRule);
150  d->customError = customError;
151 }
152 
153 void ValidatorRule::setCustomParsingError(const QString &custom)
154 {
155  Q_D(ValidatorRule);
156  d->customParsingError = custom;
157 }
158 
160 {
161  Q_D(ValidatorRule);
162  d->customValidationDataError = custom;
163 }
164 
165 bool ValidatorRule::trimBefore() const { Q_D(const ValidatorRule); return d->trimBefore; }
166 
168 {
169  Q_D(ValidatorRule);
170  d->trimBefore = trimBefore;
171 }
virtual QString genericParsingError() const
Returns a generic error message if an error occures while parsing input.
QMap< QString, QString > ParamsMultiMap
bool trimBefore() const
Returns true if field value should be trimmed before validation.
QString field() const
Returns the name of the field to validate.
QString value() const
Returns the field value.
ParamsMultiMap parameters() const
Returns the parameters to validate.
virtual QString genericValidationError() const
Returns a generic error mesage if validation failed.
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.
void setCustomError(const QString &customError)
Sets a cutom error returned with errorMessage()
void setTrimBefore(bool trimBefore)
Set to false to not trim input value before validation.
QString validationDataError() const
Returns an error message if any validation data is missing or invalid.
QString label() const
Returns the human readable field label used for generic error messages.
~ValidatorRule()
Deconstructs the ValidatorRule.
void setParameters(const ParamsMultiMap &params)
Sets the request parameters to validate.
void setLabel(const QString &label)
Sets human readable field label for generic error messages.
void setField(const QString &field)
Sets the name of the field to validate.
void setCustomParsingError(const QString &custom)
Sets a custom error message that is shown if parsing of input data fails.
virtual QString genericValidationDataError() const
Returns a generic error message if any validation data is missing or invalid.
void setCustomValidationDataError(const QString &custom)
Sets a custom error message if validation data is invalid or missing.
ValidatorRule(const QString &field, const QString &label=QString(), const QString &customError=QString())
Constructs a new ValidatorRule with given parameters and parent.
QString parsingError() const
Returns an error message if an error occured while parsing input.