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

Go to the documentation of this file.
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  * 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   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 }

Generated on Fri Jul 25 17:05:59 2008 for Wt by doxygen 1.5.3