Cutelyst  1.11.0
validatordifferent.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 "validatordifferent_p.h"
20 
21 using namespace Cutelyst;
22 
23 ValidatorDifferent::ValidatorDifferent(const QString &field, const QString &other, const QString &label, const QString &otherLabel, const QString &customError) :
24  ValidatorRule(*new ValidatorDifferentPrivate(field, other, label, otherLabel, customError))
25 {
26 }
27 
28 ValidatorDifferent::ValidatorDifferent(ValidatorDifferentPrivate &dd) :
29  ValidatorRule(dd)
30 {
31 }
32 
34 {
35 }
36 
38 {
39  QString result;
40 
41  Q_D(const ValidatorDifferent);
42 
43  const QString v = value();
44 
45  if (!v.isEmpty() && (v == d->parameters.value(d->otherField).trimmed())) {
46  result = validationError();
47  }
48 
49  return result;
50 }
51 
53 {
54  QString error;
55 
56  Q_D(const ValidatorDifferent);
57 
58  if (label().isEmpty()) {
59  error = QStringLiteral("Has to be different from the value in “%1”.").arg(!d->otherLabel.isEmpty() ? d->otherLabel : d->otherField);
60  } else {
61  error = QStringLiteral("The value in the “%1” field has to be different from the value in the “%2” field.").arg(label(), !d->otherLabel.isEmpty() ? d->otherLabel : d->otherField);
62  }
63 
64  return error;
65 }
66 
67 void ValidatorDifferent::setOtherField(const QString &otherField)
68 {
69  Q_D(ValidatorDifferent);
70  d->otherField = otherField;
71 }
72 
73 void ValidatorDifferent::setOtherLabel(const QString &otherLabel)
74 {
75  Q_D(ValidatorDifferent);
76  d->otherLabel = otherLabel;
77 }
void setOtherLabel(const QString &otherLabel)
Sets the human readable label of the other field.
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
ValidatorDifferent(const QString &field, const QString &other, const QString &label=QString(), const QString &otherLabel=QString(), const QString &customError=QString())
Constructs a new different validator.
Checks if two values are different.
QString value() const
Returns the field value.
~ValidatorDifferent()
Deconstructs the different validator.
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.
void setOtherField(const QString &otherField)
Sets the name of the other field.