00001 #include "DateValidator.h"
00002
00003 #include <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
00023 WValidator::State DateValidator::validate(WString& input, int& pos) const
00024 {
00025 WValidator::State state = WRegExpValidator::validate(input, pos);
00026
00027 std::string text = input.toUTF8();
00028
00029 if ((state == Valid) && !text.empty()) {
00030 boost::smatch what;
00031 boost::regex_match(text, what, regExp());
00032
00033 try {
00034 date d
00035 = from_string(what[3] + "/" + what[2] + "/" + what[1]);
00036
00037 if ((d >= bottom_) && (d <= top_))
00038 return Valid;
00039 else
00040 return Invalid;
00041
00042 } catch (std::exception& e) {
00043 return Invalid;
00044 }
00045 } else
00046 return state;
00047 }