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