Improvements #14234 » wt_TypedValidator.patch
| src/Wt/WDateValidator.C | ||
|---|---|---|
|
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());
|
||
|
}
|
||
| ... | ... | |
|
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);
|
||
| src/Wt/WDateValidator.h | ||
|---|---|---|
|
*/
|
||
|
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
|
||
| src/Wt/WDoubleValidator.C | ||
|---|---|---|
|
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)
|
||
| src/Wt/WDoubleValidator.h | ||
|---|---|---|
|
*/
|
||
|
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."
|
||
| src/Wt/WIntValidator.C | ||
|---|---|---|
|
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)
|
||
| src/Wt/WIntValidator.h | ||
|---|---|---|
|
*/
|
||
|
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."
|
||
| src/Wt/WTimeValidator.C | ||
|---|---|---|
|
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());
|
||
|
}
|
||
| ... | ... | |
|
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);
|
||
| src/Wt/WTimeValidator.h | ||
|---|---|---|
|
*/
|
||
|
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:
|
||