Server side validation using LotusScript

We have a document containing tens of number fields. We have client side validation but nevertheless some users (JavaScript disabled) manage to write text etc. into the number fields.

I would like to do server side validation using LotusScript because managing tens or hundreds of fields is cumbersome using formulas. As the number fields are numbered systematically they could be handled using loops in the script.

I have tried different triggers but whatever I do the http server complains “Cannot convert text to a number” before the script is run. How should I trigger the agent so that the form is validated before http task generates the error?

J

Subject: Server side validation using LotusScript

You can change the fields to text field and that will prevent the error. Then on your webquerysave you could strip out everything but numbers. It is not performing validation but it should do what you want.

Shawn

Subject: Server side validation using LotusScript

You didn’t provide us many hints. What is the code that you are using? Are you running the code in the WebQuerySave event? Via the Submit button? In field validation events?

You can dynamically get field values with script or @Functions with functions/methods like @ThisField, @ThisValue, or GetItemValue.

Subject: RE: Server side validation using LotusScript

It seems that whatever code I run it’s run after the “Cannot convert text to a number” error occurs.

I have tried to create a form containing only one number field “number01” and the following agent

Sub Initialize

Dim session As New NotesSession

Dim doc As NotesDocument

Set doc = session.DocumentContext

doc.number01 = 1

Call doc.Save(True,False)

End Sub

If I write for example number 2 to the field and click the submit button, the agent is run and field value is changed into 1. But if I write for example “test” into the field I get the error. I have tried running the agent from WebQuerySave and from the submit button with the same results.

Subject: RE: Server side validation using LotusScript

What is the field data type?

Shawn

Subject: RE: Server side validation using LotusScript

Field type is number, editable.

I have tried to run the before mentioned agent triggered by a button - no saving. But still the same error message “Cannot convert text to a number”.

Subject: RE: Server side validation using LotusScript

deleted

Subject: RE: Server side validation using LotusScript

Don’t quote me on this but I think notes will try and convert the field to a number before even running the webquerysave agent in which case you will not be able to validate it in lotusscript.

You could test this by sending yourself an email at the start of the webquerysave agent to see if the agent runs.

A way around this would be to set the field type to text and insert a hidden number field on your form. You can then set the hidden field to the number if it passes the validation in your webquerysave. You will also need to update all the existing documents in the database.

Subject: RE: Server side validation using LotusScript

I tried that and it seems that the WebQuerySave agent is not run at all if there is text in the number field.

I found http://www.codestore.net/store.nsf/cmnts/D663A10591ADC3F580256EB500429814?OpenDocument, but it assumes that the number is written actually into a text field.

I am beginning to believe that there is no way to accomplish server side validation in this case. I would like to avoid using hidden text fields because of the extra work (converting existing data, rewriting calculated fields etc.)

Subject: RE: Server side validation using LotusScript

Add a text field to the form that is hidden unless the document is in edit mode on the web and hide your existing field if the document is in edit mode on the web.

Add the CGI field Query_String_Decoded to your form and run a webqueryopen agent that checks for the value of ?EditDocument in this field. If this exists then copy the value from your numeric field to the text field as this means that an existing document is being put into edit mode from read mode. One thing is that you must not have the form property automatically enable edit mode checked in order for this to work.

Then in the webquerysave agent do your validation, copy the value to your existing numeric field and delete the new field as suggested by Shane. You will then not need to use any extra hidden fields or update the existing documents in the database.

Subject: RE: Server side validation using LotusScript

Hi to you all.

A very small contribution.

What I would do is javascript validation before submit,

in which I would delete every non-numeric characters…

Almost brutal, but very efficient. ;o)

–ian–>

Subject: RE: Server side validation using LotusScript

But unfortunately not efficient if the users have disabled JavaScript… For that reason we are looking for a server side validation solution.

Subject: RE: Server side validation using LotusScript

My suggestion would be to go with the flow and use a text field in place of a number field on the web based form but in te validation agent do an explicit conversion placing it into a number field on the document and then removing the text field off the document submitted from the web. As an example

'doc is the document in contect in the agent

if IsNotNumeric(doc.TextFieldWithNumbers) then

'…do something to show the user they failed validation

else

 doc.Numberfield = ConvertToNumeric( doc.TextFieldWithNumbers )

 call doc.RemoveItem( "TextFieldWithNumbers" )

call doc.save

end if

’ IsnotNumeric( valuesArray as variant ) as boolean

’ goes through an array of values checking for numeric or non numeric values

'ConvertToNumeric ( valueArray as variant ) as variant

Goes through the array of values, converting them to numeric using cint, cdbl or whatever) and returns the values as a scalar or variant as required.

HTH

Shane