Cutelyst  1.11.0
validatoremail.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 "validatoremail_p.h"
20 #include <QRegularExpression>
21 
22 using namespace Cutelyst;
23 
24 ValidatorEmail::ValidatorEmail(const QString &field, const QString &label, const QString &customError) :
25  ValidatorRule(*new ValidatorEmailPrivate(field, label, customError))
26 {
27 }
28 
29 ValidatorEmail::ValidatorEmail(ValidatorEmailPrivate &dd) :
30  ValidatorRule(dd)
31 {
32 }
33 
35 {
36 }
37 
38 QString ValidatorEmail::validate() const
39 {
40  QString result;
41 
42 // bool isEmail = value().contains(QRegularExpression(QStringLiteral("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")));
43 
44  if (!value().isEmpty() && !value().contains(QRegularExpression(QStringLiteral("(?(DEFINE)"
45  "(?<addr_spec> (?&local_part) @ (?&domain) )"
46  "(?<local_part> (?&dot_atom) | (?&quoted_string) | (?&obs_local_part) )"
47  "(?<domain> (?&dot_atom) | (?&domain_literal) | (?&obs_domain) )"
48  "(?<domain_literal> (?&CFWS)? \\[ (?: (?&FWS)? (?&dtext) )* (?&FWS)? \\] (?&CFWS)? )"
49  "(?<dtext> [\\x21-\\x5a] | [\\x5e-\\x7e] | (?&obs_dtext) )"
50  "(?<quoted_pair> \\\\ (?: (?&VCHAR) | (?&WSP) ) | (?&obs_qp) )"
51  "(?<dot_atom> (?&CFWS)? (?&dot_atom_text) (?&CFWS)? )"
52  "(?<dot_atom_text> (?&atext) (?: \\. (?&atext) )* )"
53  "(?<atext> [a-zA-Z0-9!#$%&'*+\\/=?^_`{|}~-]+ )"
54  "(?<atom> (?&CFWS)? (?&atext) (?&CFWS)? )"
55  "(?<word> (?&atom) | (?&quoted_string) )"
56  "(?<quoted_string> (?&CFWS)? \" (?: (?&FWS)? (?&qcontent) )* (?&FWS)? \" (?&CFWS)? )"
57  "(?<qcontent> (?&qtext) | (?&quoted_pair) )"
58  "(?<qtext> \\x21 | [\\x23-\\x5b] | [\\x5d-\\x7e] | (?&obs_qtext) )"
59  "(?<FWS> (?: (?&WSP)* \\r\\n )? (?&WSP)+ | (?&obs_FWS) )"
60  "(?<CFWS> (?: (?&FWS)? (?&comment) )+ (?&FWS)? | (?&FWS) )"
61  "(?<comment> \\( (?: (?&FWS)? (?&ccontent) )* (?&FWS)? \\) )"
62  "(?<ccontent> (?&ctext) | (?&quoted_pair) | (?&comment) )"
63  "(?<ctext> [\\x21-\\x27] | [\\x2a-\\x5b] | [\\x5d-\\x7e] | (?&obs_ctext) )"
64  "(?<obs_domain> (?&atom) (?: \\. (?&atom) )* )"
65  "(?<obs_local_part> (?&word) (?: \\. (?&word) )* )"
66  "(?<obs_dtext> (?&obs_NO_WS_CTL) | (?&quoted_pair) )"
67  "(?<obs_qp> \\\\ (?: \\x00 | (?&obs_NO_WS_CTL) | \\n | \\r ) )"
68  "(?<obs_FWS> (?&WSP)+ (?: \\r\\n (?&WSP)+ )* )"
69  "(?<obs_ctext> (?&obs_NO_WS_CTL) )"
70  "(?<obs_qtext> (?&obs_NO_WS_CTL) )"
71  "(?<obs_NO_WS_CTL> [\\x01-\\x08] | \\x0b | \\x0c | [\\x0e-\\x1f] | \\x7f )"
72  "(?<VCHAR> [\\x21-\\x7E] )"
73  "(?<WSP> [ \\t] )"
74  ")"
75  "^(?&addr_spec)$"), QRegularExpression::ExtendedPatternSyntaxOption))) {
76  result = validationError();
77  }
78 
79  return result;
80 }
81 
83 {
84  QString error;
85  if (label().isEmpty()) {
86  error = QStringLiteral("Not a valid email address.");
87  } else {
88  error = QStringLiteral("The email address in the “%1” field is not valid.").arg(label());
89  }
90  return error;
91 }
QString validate() const override
Performs the validation and returns an empty QString on success, otherwise an error message...
QString value() const
Returns the field value.
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 genericValidationError() const override
Returns a generic error message.
QString label() const
Returns the human readable field label used for generic error messages.
ValidatorEmail(const QString &field, const QString &label=QString(), const QString &customError=QString())
Constructs a new email validator.
~ValidatorEmail()
Deconstructs the email validator.