0

如果客户端处理程序(在同一个小部件上)满足验证条件,我们可以阻止服务器处理程序触发吗? 用户界面

如果文本框为空,我不希望提交执行任何服务器功能。

function doGet() {
  var app = UiApp.createApplication();
  var flex = app.createFlexTable()
     .setWidget(0, 0, app.createTextBox().setId('textbox'))
     .setWidget(0, 1, app.createButton('Submit').setId('submit'))
     .setWidget(0, 2, app.createLabel().setId('status'));

  var clientHandler = app.createClientHandler().validateNotMatches(app.getElementById('textbox'), ' ');

  var serverHandler = app.createServerHandler('submit').addCallbackElement(flex);

  app.getElementById('submit').addClickHandler(clientHandler).addClickHandler(serverHandler);

  app.add(flex);
  return app;

}

function submit(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById('status').setText('Server handler fired');
  return app;
}
4

1 回答 1

2

您在这里不需要或不需要客户端处理程序,只是服务器处理程序上的验证器:

function doGet() {
  var app = UiApp.createApplication();
  var flex = app.createFlexTable()
     .setWidget(0, 0, app.createTextBox().setId('textbox'))
     .setWidget(0, 1, app.createButton('Submit').setId('submit'))
     .setWidget(0, 2, app.createLabel().setId('status'));

  var serverHandler = app.createServerHandler('submit')
     .validateLength(app.getElementById('textbox'), 1, null)
     .addCallbackElement(flex);

  app.getElementById('submit').addClickHandler(serverHandler);

  app.add(flex);
  return app;

}

function submit(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById('status').setText('Server handler fired');
  return app;
}

如果您想要一条解释问题所在的消息,您可以添加以下内容:

var clientHandler = app.createClientHandler()
    .validateNotLength(app.getElementById('textbox'), 1, null)
    .forTargets(app.getElementById('status'))
    .setText('Cannot be empty');

app.getElementById('submit').addClickHandler(serverHandler)
    .addClickHandler(clientHandler);
于 2012-11-07T03:02:15.670 回答