« New Teamstudio Tools | Main| Is Lotus cheating? »

Improve coding for Validation

Category LotusScript UI Lists

I found myself working on a new database (opps I mean application!) tonight.  I want to create a database that will replace our paper forms for Expense reporting. This application will be for the Notes Client, but once I'm happy with it then I'll web-enable it right away. Following in the footsteps of Chris Blatnick and Nathan Freeman I actually want this database to look nice.

The first thing I want to talk about is required fields.  There a a ton of ways to require fields and the inform the user if they missed any.  Validation formulas, LotusScript, message boxes, and probably a bunch that I haven't thought of.  There are some wrong ways for sure like using validation formulas that fire on the form refresh and not on document save.  There's probably no true "right" way to do this, but in my case I want a "Pretty" way.  Oh I also want this to be EASY to code.

Let's talk about this...  

Rather then dealing with pop-up boxes, I want to do the thing that you often see on websites where the little hidden text will appear announcing that this is a required field.

A picture named M2

Ok now I know what I want it to look like.  That's usually the hard part.  "Pretty" doesn't always come naturally to us Notes Guys...

How do we achieve this?

First I'll setup the table like you would expect.  To have the fields and then a second line that simply has hidden text.  I'll make the text blue to stand out.  (You do know that Red is the most likely color for people to be color blind against right?)
A picture named M3

The next thing we need to do is create some computed for display fields to individually turn on or off the text warnings.  I put a hidden table at the top of my form to hold these fields.  Here are all the fields I'm requiring so far.

A picture named M4

Ok I admit so far this isn't rocket science.  Hang in there....  it get's better!

What's the next step?  I need to put in the text properties of the different Required Field's a hide when formula.  Here the one for the amount warning.
A picture named M5

So what's the last step?  In the QuerySave event I now just need to inspect the field, and if there's no value in "amount" for example, then I just place ANYTHING in the req_amount field to turn the warning on.  I refresh the document and BAM! I get my warning.  If there is a value then I make sure req_amount is clear and BAM again!  No warning.  That code might look something like this:

A picture named M6

Now I basically repeat these 5 lines for each field changing the names where appropriate.  Let's see that's 25 lines so far...  But what if I had 10 fields?  50 lines?  Boy it probably should be moved out of query save and put in a function...  What if I had 15 fields?  or more?  So I really want to type all that in?  Heck no!  We can be a little more clever then that!

Most of us use dot notation right?  You saw it in the last screenshot with doc.amount(0).  But there is another way to get the SAME value.  The long way I guess you call it.
doc.amount(0) is the same is doc.getitemvalue(fieldname)(0).

The BEAUTY of getting fields the long way is you can use VARIABLES.  Oh and Bob Balaban says that it's a little quicker too.  And while I think he was proven wrong in a recent version of Notes, if Bob said it then that's good enough for me!

How does variables help in this situation?  All the code is the same except for two variables..  The field we want to inspect and the field that controls the hide when.  These fields go together like peanut butter and chocolate... 2 fields that go together...  hmmm.. 2 items that go together.... 2... Two... there's something about 2 things that go together....  Say it with me now...

LISTS!!!  

What if we used a list to hold our pairs of inspecting fields and control fields.  Then maybe we could use a single loop and inspect everything at once.  Then we could inspect as MANY fields as we want.  Check out this screenshot!


A picture named M7

Now we can add as many pairs to the list that we want and not have to do individual if then's for each of them.

Naturally this is just validating for a value and not for specific requirements in a field.  But still I think this is fairly easy to implement, very maintainable, and gives a nice pretty look.

Any comments would be appreciated!

Post A Comment

:-D:-o:-p:-x:-(:-):-\:angry::cool::cry::emb::grin::huh::laugh::lips::rolleyes:;-)

Powered By:

Domino BlogSphere
Version 3.0.1 Beta 8

Stuff