diff --git a/src/Wt/WDateValidator.C b/src/Wt/WDateValidator.C index 0e046cbb..cff9bfe9 100644 --- a/src/Wt/WDateValidator.C +++ b/src/Wt/WDateValidator.C @@ -144,17 +144,8 @@ WValidator::Result WDateValidator::validate(const WT_USTRING& input) const try { WDate d = WDate::fromString(input, formats_[i]); - if (d.isValid()) { - if (!bottom_.isNull()) - if (d < bottom_) - return Result(ValidationState::Invalid, invalidTooEarlyText()); + return validate(d); - if (!top_.isNull()) - if (d > top_) - return Result(ValidationState::Invalid, invalidTooLateText()); - - return Result(ValidationState::Valid); - } } catch (std::exception& e) { LOG_WARN("validate(): " << e.what()); } @@ -163,6 +154,26 @@ WValidator::Result WDateValidator::validate(const WT_USTRING& input) const return Result(ValidationState::Invalid, invalidNotADateText()); } +WValidator::Result WDateValidator::validate(WDate d) const +{ + if(d.isNull()) + return WValidator::validate(""); + + if (d.isValid()) { + if (!bottom_.isNull()) + if (d < bottom_) + return Result(ValidationState::Invalid, invalidTooEarlyText()); + + if (!top_.isNull()) + if (d > top_) + return Result(ValidationState::Invalid, invalidTooLateText()); + + return Result(ValidationState::Valid); + } + + return Result(ValidationState::Invalid, invalidNotADateText()); +} + void WDateValidator::loadJavaScript(WApplication *app) { LOAD_JAVASCRIPT(app, "js/WDateValidator.js", "WDateValidator", wtjs1); diff --git a/src/Wt/WDateValidator.h b/src/Wt/WDateValidator.h index 356d37fb..5988700f 100644 --- a/src/Wt/WDateValidator.h +++ b/src/Wt/WDateValidator.h @@ -137,6 +137,13 @@ public: */ virtual Result validate(const WT_USTRING& input) const override; + /*! @brief Validates the given input + * + * The input is considered valid only when input is empty for a + * non-mandatory field, or within the valid range + */ + Result validate(WDate input) const; + /*! \brief Sets the message to display when the input is not a date. * * The default message is "The date must be of the format {1}", with diff --git a/src/Wt/WDoubleValidator.C b/src/Wt/WDoubleValidator.C index 31fe47e4..ec23f65f 100644 --- a/src/Wt/WDoubleValidator.C +++ b/src/Wt/WDoubleValidator.C @@ -128,15 +128,26 @@ WValidator::Result WDoubleValidator::validate(const WT_USTRING& input) const try { double i = WLocale::currentLocale().toDouble(text); + return validate(i); + + } catch (std::exception& e) { + return Result(ValidationState::Invalid, invalidNotANumberText()); + } +} + +WValidator::Result WDoubleValidator::validate(double i) const +{ + if(isMandatory()) + { if (i < bottom_) return Result(ValidationState::Invalid, invalidTooSmallText()); else if (i > top_) return Result(ValidationState::Invalid, invalidTooLargeText()); else return Result(ValidationState::Valid); - } catch (std::exception& e) { - return Result(ValidationState::Invalid, invalidNotANumberText()); } + else + return Result(ValidationState::Valid); } void WDoubleValidator::loadJavaScript(WApplication *app) diff --git a/src/Wt/WDoubleValidator.h b/src/Wt/WDoubleValidator.h index 8f644549..e1d21637 100644 --- a/src/Wt/WDoubleValidator.h +++ b/src/Wt/WDoubleValidator.h @@ -89,6 +89,13 @@ public: */ virtual Result validate(const WT_USTRING& input) const override; + /*! \brief Validates the given input. + * + * The input is considered valid only when it + * represents a double within the valid range, or any value for non mandatory Validator + */ + Result validate(double input) const; + /*! \brief Sets the message to display when the input is not a number. * * The default value is "Must be a number." diff --git a/src/Wt/WIntValidator.C b/src/Wt/WIntValidator.C index 8044e602..cbc8fa7c 100644 --- a/src/Wt/WIntValidator.C +++ b/src/Wt/WIntValidator.C @@ -127,15 +127,26 @@ WValidator::Result WIntValidator::validate(const WT_USTRING& input) const try { int i = WLocale::currentLocale().toInt(text); + return validate(i); + + } catch (std::exception& e) { + return Result(ValidationState::Invalid, invalidNotANumberText()); + } +} + +WValidator::Result WIntValidator::validate(int i) const +{ + if(isMandatory()) + { if (i < bottom_) return Result(ValidationState::Invalid, invalidTooSmallText()); else if (i > top_) return Result(ValidationState::Invalid, invalidTooLargeText()); else return Result(ValidationState::Valid); - } catch (std::exception& e) { - return Result(ValidationState::Invalid, invalidNotANumberText()); } + else + return Result(ValidationState::Valid); } void WIntValidator::loadJavaScript(WApplication *app) diff --git a/src/Wt/WIntValidator.h b/src/Wt/WIntValidator.h index df1d9f6f..f4299fb0 100644 --- a/src/Wt/WIntValidator.h +++ b/src/Wt/WIntValidator.h @@ -88,6 +88,13 @@ public: */ virtual Result validate(const WT_USTRING& input) const override; + /*! \brief Validates the given input. + * + * The input is considered valid only when it is any number for a non-mandatory + * field, or represents an integer within the valid range. + */ + Result validate(int input) const; + /*! \brief Sets the message to display when the input is not a number. * * The default value is "Must be an integer number." diff --git a/src/Wt/WTimeValidator.C b/src/Wt/WTimeValidator.C index c7453a2e..68ecc58d 100644 --- a/src/Wt/WTimeValidator.C +++ b/src/Wt/WTimeValidator.C @@ -170,21 +170,9 @@ WValidator::Result WTimeValidator::validate(const WT_USTRING &input) const for (unsigned i = 0; i < formats_.size(); i++){ try { WTime t = WTime::fromString(input, formats_[i]); - if(t.isValid()){ - if(!bottom_.isNull() && t < bottom_) - return Result(ValidationState::Invalid, - invalidTooEarlyText()); - if(!top_.isNull() && t > top_) - return Result(ValidationState::Invalid, - invalidTooLateText()); - if (step_ > std::chrono::seconds(0)) { - WTime start = !bottom_.isNull() ? bottom_ : WTime(0, 0); - long secs = start.secsTo(t); - if (secs % step_.count() != 0) - return Result(ValidationState::Invalid, invalidWrongStepText()); - } - return Result(ValidationState::Valid); - } + + return validate(t); + } catch (std::exception &e){ LOG_WARN("validate(): " << e.what()); } @@ -192,6 +180,32 @@ WValidator::Result WTimeValidator::validate(const WT_USTRING &input) const return Result(ValidationState::Invalid, invalidNotATimeText()); } + +WValidator::Result WTimeValidator::validate(const WTime &t) const +{ + if(t.isNull()) + return WValidator::validate(""); + + if(t.isValid()) + { + if(!bottom_.isNull() && t < bottom_) + return Result(ValidationState::Invalid, + invalidTooEarlyText()); + if(!top_.isNull() && t > top_) + return Result(ValidationState::Invalid, + invalidTooLateText()); + if (step_ > std::chrono::seconds(0)) { + WTime start = !bottom_.isNull() ? bottom_ : WTime(0, 0); + long secs = start.secsTo(t); + if (secs % step_.count() != 0) + return Result(ValidationState::Invalid, invalidWrongStepText()); + } + return Result(ValidationState::Valid); + } + + return Result(ValidationState::Invalid, invalidNotATimeText()); +} + void WTimeValidator::loadJavaScript(WApplication *app) { LOAD_JAVASCRIPT(app, "js/WTimeValidator.js", "WTimeValidator", wtjs1); diff --git a/src/Wt/WTimeValidator.h b/src/Wt/WTimeValidator.h index 138ab09a..b09b130f 100644 --- a/src/Wt/WTimeValidator.h +++ b/src/Wt/WTimeValidator.h @@ -126,6 +126,14 @@ public: */ virtual Result validate(const WT_USTRING &input) const override; + /*! \brief Validates the given input + * + * The input is considered valid only when it is blank for a + * non-mandatory field, or represents a time in the given format, + * and within the valid range. + */ + Result validate(const WTime& input) const; + virtual std::string javaScriptValidate() const override; private: