2012-06-21 29 views

回答

0

我想有幾種可能性,其中之一是使用長度驗證

var clientHandler = app.createClientHandler().validateLength(widget, min, max) 

,並以此來setVisible警告消息,並禁用「提交按鈕」最終...

documentation on clientHandlers是相當明確的

以下是在一個簡單的表格,檢查question 1並顯示警告消息的示例:

function BuildForm() { 
    var text= new Array(); 
    text =['question 1','question 2','question 3','question 4','question 5','question 6']; 
    var textBox = new Array();  
    var app=UiApp.createApplication().setTitle('Form'); 
    var panel = app.createVerticalPanel().setId('panel'); 
    var btn = app.createButton('process'); 
    var warning = app.createLabel('You forgot to fill in question 1').setVisible(false) 
    for (nn=0;nn<text.length;++nn){ 
    var label = app.createLabel(text[nn]); 
    textBox[nn] = app.createTextBox().setName(text[nn].replace(' ','')); 
    panel.add(label).add(textBox[nn]) 
    } 
    var cliH1 = app.createClientHandler().validateLength(textBox[0], 0, 1) 
     .forTargets(btn).setEnabled(false) 
     .forTargets(warning).setVisible(true) 
    var cliH2 = app.createClientHandler() 
     .forTargets(btn).setEnabled(true) 
     .forTargets(warning).setVisible(false) 

    var servH = app.createServerHandler('process').addCallbackElement(panel).validateLength(textBox[0], 1, 40) 
    btn.addClickHandler(cliH1) 
    textBox[0].addClickHandler(cliH2) 
    btn.addClickHandler(servH) 
    panel.add(btn).add(warning) 
    app.add(panel) 
    var doc = SpreadsheetApp.getActive(); 
    doc.show(app) 
} 


function process(e){ 
    var app = UiApp.getActiveApplication() 
     Browser.msgBox(e.parameter.question1+' '+e.parameter.question2+' '+e.parameter.question3+' '+e.parameter.question4+' '+e.parameter.question5+' '+e.parameter.question6) 
     app.close() 
     return app 
      } 

編輯:這是使用另一個驗證(validateMatches),並檢查所有其他版本的答案

function BuildForm2() { 
    var text= new Array(); 
    text =['question 1','question 2','question 3','question 4','question 5','question 6']; 
    var textBox = new Array();  
    var app=UiApp.createApplication().setTitle('Form'); 
    var panel = app.createVerticalPanel().setId('panel'); 
    var btn = app.createButton('process').setWidth('240'); 
    var warning = app.createLabel('You forgot to fill at least one question').setVisible(false) 
    for (nn=0;nn<text.length;++nn){ 
    var label = app.createLabel(text[nn]); 
    textBox[nn] = app.createTextBox().setName(text[nn].replace(/ /g,'')); 
    panel.add(label).add(textBox[nn]) 
    } 
    var servH = app.createServerHandler('process').addCallbackElement(panel).validateLength(textBox[0], 1, 40).validateLength(textBox[1], 1, 40).validateLength(textBox[2], 1, 40) 
     .validateLength(textBox[3], 1, 40).validateLength(textBox[4], 1, 40).validateLength(textBox[5], 1, 40); 
    var cliH = app.createClientHandler().validateMatches(textBox[0],'').validateMatches(textBox[1],'').validateMatches(textBox[2],'').validateMatches(textBox[3],'') 
     .validateMatches(textBox[4],'').validateMatches(textBox[5],'') 
     .forTargets(warning).setVisible(true) 
    btn.addClickHandler(servH).addClickHandler(cliH) 
    panel.add(btn).add(warning) 
    app.add(panel) 
    var doc = SpreadsheetApp.getActive(); 
    doc.show(app) 
}