WValidator::Result WTimeValidator::validate(const WT_USTRING& input) const { if (input.empty()) return WValidator::validate(input); for (unsigned i = 0; i < formats_.size(); ++i) { try { WTime d = WTime::fromString(input, formats_[i]); if (d.isValid()) { if (!bottom_.isNull()) if (d < bottom_) return Result(Invalid, invalidTooEarlyText()); if (!top_.isNull()) if (d > top_) return Result(Invalid, invalidTooLateText()); return Result(Valid); } } catch (std::exception& e) { LOG_WARN("validate(): " << e.what()); } } return Result(Invalid, invalidNotATimeText()); }
std::string WTimeValidator::javaScriptValidate() const { loadJavaScript(WApplication::instance()); WStringStream js; js << "new " WT_CLASS ".WTimeValidator(" << (isMandatory() ? "true" : "false") << ",["; for (unsigned i = 0; i < formats_.size(); ++i) { WTime::RegExpInfo r = WTime::formatToRegExp(formats_[i]); if (i != 0) js << ','; js << "{" << "regexp:" << WWebWidget::jsStringLiteral(r.regexp) << ',' << "getHour:function(results){" << r.hourGetJS << ";}," << "getMinute:function(results){" << r.minuteGetJS << ";}," << "getSecond:function(results){" << r.secGetJS << ";}" << "getMSec:function(results){" << r.msecGetJS << ";}" << "}"; } js << "],"; if (!bottom_.isNull()) js << "new Time(" << bottom_.hour() << ',' << bottom_.minute()-1 << ',' << bottom_.second() << ")"; else js << "null"; js << ','; if (!top_.isNull()) js << "new Time(" << top_.hour() << ',' << top_.minute()-1 << ',' << top_.second() << ")"; else js << "null"; js << ',' << invalidBlankText().jsStringLiteral() << ',' << invalidNotATimeText().jsStringLiteral() << ',' << invalidTooEarlyText().jsStringLiteral() << ',' << invalidTooLateText().jsStringLiteral() << ");"; return js.str(); }
std::string WDateValidator::javaScriptValidate() const { loadJavaScript(WApplication::instance()); WStringStream js; js << "new " WT_CLASS ".WDateValidator(" << isMandatory() << ",["; for (unsigned i = 0; i < formats_.size(); ++i) { WDate::RegExpInfo r = WDate::formatToRegExp(formats_[i]); if (i != 0) js << ','; js << "{" << "regexp:" << WWebWidget::jsStringLiteral(r.regexp) << ',' << "getMonth:function(results){" << r.monthGetJS << ";}," << "getDay:function(results){" << r.dayGetJS << ";}," << "getYear:function(results){" << r.yearGetJS << ";}" << "}"; } js << "],"; if (!bottom_.isNull()) js << "new Date(" << bottom_.year() << ',' << bottom_.month()-1 << ',' << bottom_.day() << ")"; else js << "null"; js << ','; if (!top_.isNull()) js << "new Date(" << top_.year() << ',' << top_.month()-1 << ',' << top_.day() << ")"; else js << "null"; js << ',' << invalidBlankText().jsStringLiteral() << ',' << invalidNotADateText().jsStringLiteral() << ',' << invalidTooEarlyText().jsStringLiteral() << ',' << invalidTooLateText().jsStringLiteral() << ");"; return js.str(); }