|
#include <Wt/WLineEdit>
|
|
#include <Wt/WValidator>
|
|
#include <Wt/WPushButton>
|
|
#include <Wt/WApplication>
|
|
#include <Wt/WBootstrapTheme>
|
|
#include <Wt/WContainerWidget>
|
|
#include <Wt/WTemplateFormView>
|
|
|
|
#ifndef WT_DEBUG_JS
|
|
#include "MatchValidator.js"
|
|
#endif
|
|
|
|
using namespace Wt;
|
|
|
|
class MatchValidator : public WValidator
|
|
{
|
|
public:
|
|
MatchValidator(WFormWidget *pCompareWidget, WObject *pParent = 0)
|
|
: WValidator(pParent), m_pCompareWidget(pCompareWidget)
|
|
{
|
|
//assert(m_pFormWidget);
|
|
}
|
|
|
|
MatchValidator(WFormWidget *pCompareWidget, bool bMandatory, WObject *pParent = 0)
|
|
: WValidator(bMandatory, pParent), m_pCompareWidget(pCompareWidget)
|
|
{
|
|
//assert(m_pFormWidget);
|
|
}
|
|
|
|
virtual ~MatchValidator()
|
|
{
|
|
}
|
|
|
|
void SetCompareWidget(WFormWidget *pCompareWidget)
|
|
{
|
|
m_pCompareWidget = pCompareWidget;
|
|
}
|
|
|
|
WFormWidget* CompareWidget()
|
|
{
|
|
return m_pCompareWidget;
|
|
}
|
|
|
|
void setInvalidDataText(const WString& text)
|
|
{
|
|
m_strInvalidDataText = text;
|
|
}
|
|
|
|
WString invalidDataText() const
|
|
{
|
|
if(m_strInvalidDataText.empty()) {
|
|
return WString::tr("MISMATCH_INVALID_DATA_ERROR");
|
|
}
|
|
|
|
return m_strInvalidDataText;
|
|
}
|
|
|
|
void setMismatchText(const WString& text)
|
|
{
|
|
m_strMismatchText = text;
|
|
}
|
|
|
|
WString mismatchText() const
|
|
{
|
|
if(m_strMismatchText.empty()) {
|
|
return WString::tr("MISMATCH_ERROR");
|
|
}
|
|
|
|
return m_strMismatchText;
|
|
}
|
|
|
|
WValidator::Result validate(const WT_USTRING& strInput) const
|
|
{
|
|
if(nullptr == m_pCompareWidget) {
|
|
return Result(Invalid, invalidDataText());
|
|
}
|
|
|
|
if(strInput.empty()) {
|
|
return WValidator::validate(strInput);
|
|
}
|
|
|
|
if(strInput != m_pCompareWidget->valueText()) {
|
|
return Result(Invalid, mismatchText());
|
|
}
|
|
|
|
return Result(Valid);
|
|
}
|
|
|
|
std::string javaScriptValidate() const
|
|
{
|
|
//assert(m_pFormWidget);
|
|
|
|
loadJavaScript(WApplication::instance());
|
|
|
|
WStringStream js;
|
|
|
|
js << "new " WT_CLASS ".MatchValidator(";
|
|
|
|
if(nullptr == m_pCompareWidget) {
|
|
js << "null";
|
|
} else {
|
|
js << m_pCompareWidget->jsRef();
|
|
}
|
|
|
|
js << ',' << isMandatory() << ','
|
|
<< invalidBlankText().jsStringLiteral() << ','
|
|
<< invalidDataText().jsStringLiteral() << ','
|
|
<< mismatchText().jsStringLiteral() << ");";
|
|
|
|
return js.str();
|
|
}
|
|
|
|
#ifndef WT_TARGET_JAVA
|
|
void createExtConfig(std::ostream& config) const
|
|
{
|
|
if(!invalidDataText().empty()) {
|
|
config << ",invalidData:" << invalidDataText().jsStringLiteral();
|
|
}
|
|
|
|
if(!mismatchText().empty()) {
|
|
config << ",mismatch:" << mismatchText().jsStringLiteral();
|
|
}
|
|
|
|
WValidator::createExtConfig(config);
|
|
}
|
|
#endif //WT_TARGET_JAVA
|
|
|
|
protected:
|
|
static void loadJavaScript(WApplication *app)
|
|
{
|
|
LOAD_JAVASCRIPT(app, "MatchValidator.js", "MatchValidator", wtjs1);
|
|
}
|
|
|
|
private:
|
|
WFormWidget *m_pCompareWidget;
|
|
WString m_strInvalidDataText;
|
|
WString m_strMismatchText;
|
|
};
|
|
|
|
class MatchValidatorTestView : public WTemplateFormView
|
|
{
|
|
public:
|
|
MatchValidatorTestView(WContainerWidget *pParent = 0)
|
|
: WTemplateFormView(tr("MatchValidator.TestView"), pParent)
|
|
{
|
|
updateView(&m_cModel);
|
|
}
|
|
|
|
protected:
|
|
WFormWidget* createFormWidget(WFormModel::Field field)
|
|
{
|
|
if(Model::W1 == field) {
|
|
WLineEdit *w1 = new WLineEdit();
|
|
|
|
MatchValidator *v = dynamic_cast<MatchValidator*>(m_cModel.validator(Model::W2));
|
|
if(v != nullptr) {
|
|
v->SetCompareWidget(w1);
|
|
}
|
|
|
|
return w1;
|
|
} else if(Model::W2 == field) {
|
|
return new WLineEdit();
|
|
} else if(Model::TEST == field) {
|
|
WPushButton *pTest = new WPushButton("Test");
|
|
pTest->clicked().connect(this, &MatchValidatorTestView::TestClicked);
|
|
return pTest;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
class Model : public WFormModel
|
|
{
|
|
public:
|
|
static constexpr WFormModel::Field W1 = "W1";
|
|
static constexpr WFormModel::Field W2 = "W2";
|
|
static constexpr WFormModel::Field TEST = "TEST";
|
|
|
|
Model(WObject *pParent = 0)
|
|
: WFormModel(pParent)
|
|
{
|
|
addField(W1);
|
|
addField(W2);
|
|
addField(TEST);
|
|
|
|
setValidator(W2, new MatchValidator(nullptr, true));
|
|
}
|
|
};
|
|
|
|
private:
|
|
void TestClicked()
|
|
{
|
|
updateModel(&m_cModel);
|
|
|
|
if(m_cModel.validate()) {
|
|
updateView(&m_cModel);
|
|
} else {
|
|
updateView(&m_cModel);
|
|
}
|
|
}
|
|
|
|
Model m_cModel;
|
|
};
|
|
|
|
class MatchValidatorApp : public WApplication
|
|
{
|
|
public:
|
|
MatchValidatorApp(const WEnvironment& env)
|
|
: WApplication(env)
|
|
{
|
|
setTheme(new WBootstrapTheme());
|
|
messageResourceBundle().use(appRoot() + "template");
|
|
messageResourceBundle().use(appRoot() + "strings");
|
|
|
|
addMetaHeader("viewport", "width = device-width, initial-scale = 1");
|
|
|
|
root()->addStyleClass("container-fluid");
|
|
|
|
new MatchValidatorTestView(root());
|
|
}
|
|
};
|
|
|
|
WApplication* CreateApplication(const WEnvironment& env)
|
|
{
|
|
return new MatchValidatorApp(env);
|
|
}
|
|
|
|
int main(int iArgCount, char **ppArgs)
|
|
{
|
|
return WRun(iArgCount, ppArgs, &CreateApplication);
|
|
}
|
|
|