Go to the documentation of this file.00001 #include "DateValidator.h"
00002
00003 #include <Wt/WString>
00004 #include <boost/regex.hpp>
00005 #include <boost/date_time/gregorian/gregorian.hpp>
00006
00007 using namespace boost::gregorian;
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 DateValidator::DateValidator(const date& bottom, const date& top)
00019 : WRegExpValidator("(\\d{1,2})/(\\d{1,2})/(\\d{4})"),
00020 bottom_(bottom),
00021 top_(top)
00022 {
00023 setNoMatchText("Must be a date in format 'dd/MM/yyyy'");
00024 }
00025
00026 WValidator::State DateValidator::validate(WString& input) const
00027 {
00028 WValidator::State state = WRegExpValidator::validate(input);
00029
00030 std::string text = input.toUTF8();
00031
00032 if ((state == Valid) && !text.empty()) {
00033 boost::smatch what;
00034 boost::regex_match(text, what, boost::regex(regExp().toUTF8()));
00035
00036 try {
00037 date d
00038 = from_string(what[3] + "/" + what[2] + "/" + what[1]);
00039
00040 if ((d >= bottom_) && (d <= top_))
00041 return Valid;
00042 else
00043 return Invalid;
00044
00045 } catch (std::exception& e) {
00046 return Invalid;
00047 }
00048 } else
00049 return state;
00050 }