0

我试图了解处理程序如何在 Google 的 UI 服务中进行验证。

如果我有一个带有按钮的文本框,并且我只想在文本框为 1)非空且 2)包含特定值(例如“Fred”)时联系服务器,我如何创建一个验证函数来检查在允许服务器处理程序触发之前,值是“Fred”吗?

一些示例代码:

function myValid() {

 //create the app
 var app = UiApp.createApplication();

 //set out UI in table
 var flex = app.createFlexTable()
  .setWidget(0, 0, app.createTextBox().setName('textbox').setId('textbox'))
  .setWidget(0, 1, app.createButton('Submit').setId('submit'))
  .setWidget(0, 2, app.createLabel().setId('status'));

 //server handler - fires only if textbox isn't empty
 var serverHandler = app.createServerHandler('submit')
  .validateLength(app.getElementById('textbox'), 1, null)
  .addCallbackElement(flex);

 //my custom handler to check the value of the textbox
 var myserverHandler = app.createServerHandler('myCheck')
 .addCallbackElement(flex);

 //client handler to display a message if textbox is empty
 var clientHandler = app.createClientHandler()
 .validateNotLength(app.getElementById('textbox'), 1, null)
 .forTargets(app.getElementById('status'))
 .setText('Cannot be empty');

  //add the handlers to the submit button
  app.getElementById('submit')
  .addClickHandler(serverHandler)
  .addClickHandler(myserverHandler)
  .addClickHandler(clientHandler);

 //add table to UI
 app.add(flex);
 //show app in the current spreadsheet
 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
 spreadsheet.show(app);
}

//check to see if textbox value is 'Fred'. If not display error

function myCheck(e) {
 var app = UiApp.getActiveApplication();

 if (e.parameter.textbox != "Fred")
  var errorLabel = app.createLabel("Incorrect Value");
 app.add(errorLabel);

 return app;
}

//If all validation passes then display message

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

我试图使用服务器处理程序来检查文本框的值,但客户端处理程序会更好。如何编写自定义客户端处理程序来检查“Fred”的文本框值?另外,在满足所有验证条件之前,如何防止服务器处理程序触发?

谢谢

4

1 回答 1

0

该解决方案比您的代码更简单,一个服务器处理程序就足够了,只需对“Fred”进行一次验证,而 ClientHandler 使用validateNotMatches.

代码如下。

function myValid() {
  var app = UiApp.createApplication();
  var textBox = app.createTextBox().setName('textbox').setId('textbox');
  var btn = app.createButton('Submit');
  var label = app.createLabel().setId('status');
  var flex = app.createFlexTable()
  .setWidget(0, 0, textBox)
  .setWidget(0, 1, btn)
  .setWidget(0, 2, label);
  var serverHandler = app.createServerHandler('submit')
  .validateMatches(textBox, 'Fred')
  .addCallbackElement(flex);
  var clientHandler = app.createClientHandler()
  .validateNotMatches(textBox, 'Fred').forTargets(label).setText('Missing or invalid value');
  btn.addClickHandler(serverHandler).addClickHandler(clientHandler);
  app.add(flex);
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.show(app);
}

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

此代码接受任何“包含”Fred 的答案,如果您希望它拒绝除“Fred”以外的任何内容,只需在 textBox length == 4 ( validateLength,textBox,4,4) 上添加验证,我不确定您真正想要什么...


编辑:感谢您的接受,这个编辑只是提到还有另一种您可能感兴趣的方法btn.setEnabled()KeyUpHandler下面的代码......选择是你的;-)

function myValid() {
  var app = UiApp.createApplication();
  var textBox = app.createTextBox().setName('textbox').setId('textbox');
  var btn = app.createButton('Submit').setEnabled(false);
  var label = app.createLabel().setId('status');
  var flex = app.createFlexTable()
  .setWidget(0, 0, textBox)
  .setWidget(0, 1, btn)
  .setWidget(0, 2, label);
  var serverHandler = app.createServerHandler('submit')
  .validateMatches(textBox, 'Fred')
  .addCallbackElement(flex);
  btn.addClickHandler(serverHandler);
  var btnHandler = app.createClientHandler().validateMatches(textBox,'Fred').forTargets(btn).setEnabled(true);
  textBox.addKeyUpHandler(btnHandler);
  app.add(flex);
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  spreadsheet.show(app);
}

function submit(e) {
  var app = UiApp.getActiveApplication();
  app.getElementById('status').setText('Server handler fired');
  return app;
}
于 2013-11-13T07:13:07.227 回答