2

I want to tie a ServerHandler to a submit button so I can do validation of my form prior to submission. If the validation succeeds, then the POST of the form will be carried out. If the validation fails (due to an incomplete or incorrect field), then the POST will be stopped, and the ServerHandler can indicate the incorrect fields. The kind of thing you do in Javascript with event.preventDefault(), or returning false in the button handler for a submit.

var app = UIApp.getActiveApplication();
var form = app.createFormPanel();
var panel = app.createVerticalPanel();
// Add form elements to panel such as textboxes, radio buttons, list boxes etc.
...
...

var button = app.createSubmitButton("submit");
button.addClickHandler(app.createServerHandler("validate").addCallbackElement(panel));
panel.add(button);
form.add(panel);
app.add(form);


function validate(e) {
    // Do the validation of the form

    // QUESTION - How do we then disable or allow the POST?
}

So the kludge I'm currently doing is to have a validate button (normal button), which gets changed to a 'submit' button if the form validation done in the serverhandler is good. The user then has to click the button again to submit the form. I have to lock all the elements to prevent them changing any fields once the validation is good. There has to be a better way more akin to the Javascript route mentioned above, and referenced here: event.preventDefault() vs. return false

For reference, if you tie the validation ServerHandler to a submit button, it will always do a POST.

Community
  • 1
  • 1
user1507720
  • 211
  • 3
  • 10
  • Sounds as though a Client Handler might be in order. – ScampMichael Jan 29 '13 at 16:34
  • @user1507720 why don't you do everything within the (normal) button and you control what en when something goes to your spreadsheet. Take a look at this [github example](https://github.com/Jacobvdb/gdg-event-administration/blob/master/gdg/add_gdg.gs) which does all the validation in the (normal) button. – Jacobvdb Jan 30 '13 at 22:59

0 Answers0