I\'m looking to do validation / required fields on a form that has text input and fileupload for file attachments.
The script takes inputs and allows user to attach
One simple way to do it is to use another client handler with validators. I suggest to put it on the fileUpload like this : (replace in your code at the same place)
var cliHandler2 = app.createClientHandler().validateLength(loanAmountTextbox, 1, 20)
.validateLength(borrowerTextbox, 1, 20).validateLength(lienPos, 1, 20).forTargets(submitButton).setEnabled(true);// you can add more conditions here (widget name, minimum length, max length)
var upLoad = app.createFileUpload().setName('thefile').addChangeHandler(cliHandler2);
and this one enables the submit button! you can test it here
EDIT
Here is the more sophisticated option that checks all the widgets on client handler.... I reproduce the whole relevant part and update the online example - EDIT3 : the DateBox validation doesn't work, that's an issue we'll have to go through ! in the mean time I added a new message handling on the same handler
EDIT 4 : (last one !)
I finally found a working solution for each widget type, the date must contain a '2' (which will be true for a couple of years I think ;-) I show the whole doGet function because I made some other changes here and there... It works best when the fileUpload widget is filled in last position (don't know why) and in certain situations one need to re-modify a textBow to get the validation but in most cases it's working as it should.
function doGet(e) {
var app = UiApp.createApplication().setTitle('Loan Registration Processing');
var panel = app.createFormPanel();
var grid = app.createGrid(8,2).setId('loanGrid');
var loanList = app.createListBox().setName('Loan List').setWidth('120px').setName('LoanType');
loanList.addItem('Select Option');
loanList.addItem('FHA');
loanList.addItem('Convential');
loanList.addItem('VA');
loanList.addItem('Reverse');
loanList.addItem('HELOC');
var borrowerTextbox = app.createTextBox().setWidth('150px').setName('borrower');
var loanAmountTextbox = app.createTextBox().setWidth('150px').setName('amount');
var appDatebox = app.createDateBox().setWidth('150px').setName('date');
var lienPos = app.createListBox().setName('Lien Position').setWidth('150px').setName('LienPosition');
lienPos.addItem('Select Option');
lienPos.addItem('1st');
lienPos.addItem('2nd');
var propType = app.createListBox().setName('Property Type').setWidth('150px').setName('PropertyType');
propType.addItem('Select Option');
propType.addItem('1-4');
propType.addItem('Manufactured');
var submitButton = app.createSubmitButton('<B>Submit</B>');
var warning = app.createHTML('Please fill in all fields').setStyleAttribute('background','#FFcc99').setStyleAttribute('fontSize','20px');
//file upload
var upLoad = app.createFileUpload().setName('thefile');
var cliHandler2 = app.createClientHandler()
.validateLength(borrowerTextbox, 1, 40).validateLength(loanAmountTextbox, 1, 40).validateNotMatches(loanList,'Select Option')
.validateNotMatches(lienPos,'Select Option').validateNotMatches(propType, 'Select Option').validateMatches(appDatebox, '2','g')
.validateNotMatches(upLoad, 'FileUpload').forTargets(submitButton).setEnabled(true).forTargets(warning)
.setHTML('Now you can submit your form').setStyleAttribute('background','#99FF99').setStyleAttribute('fontSize','12px')
//Grid layout of items on form
grid.setText(0, 0, 'Loan Type')
.setWidget(0, 1, loanList.addClickHandler(cliHandler2))
.setText(1, 0, "Borrower's Name")
.setWidget(1, 1, borrowerTextbox.addKeyUpHandler(cliHandler2))
.setText(2, 0, 'Loan Amount')
.setWidget(2, 1, loanAmountTextbox.addKeyUpHandler(cliHandler2))
.setText(3, 0, 'Loan Date')
.setWidget(3, 1, appDatebox)
.setText(4, 0, 'Lien Position')
.setWidget(4, 1, lienPos.addClickHandler(cliHandler2))
.setText(5, 0, 'Property Type')
.setWidget(5, 1, propType.addClickHandler(cliHandler2))
.setText(6, 0, 'File Upload')
.setWidget(6, 1, upLoad.addChangeHandler(cliHandler2))
.setWidget(7, 0, submitButton)
.setWidget(7, 1, warning);
var cliHandler = app.createClientHandler().forTargets(warning).setHTML('<B>PLEASE WAIT WHILE DATA IS UPLOADING<B>').setStyleAttribute('background','yellow');
submitButton.addClickHandler(cliHandler).setEnabled(false);
panel.add(grid);
app.add(panel);
return app;
}