/home/koen/project/wt/cvs/wt/examples/form/DateValidator.C

Go to the documentation of this file.
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  * Disclaimer: I am clueless how to use boost::gregorian in a sensible way.
00010  *
00011  * That, together with the fact that I wanted to test WRegExpValiator
00012  * is the reason why I use a regular expression to get the
00013  * day/month/year fields, and boost::gregorian to check that the date
00014  * is a valid date.
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 }

Generated on Mon Apr 14 15:15:04 2008 for Wt by doxygen 1.5.3