Form validation
Posted: Fri Aug 07, 2009 10:58 am
by Dave Mellow >> Thu, 17 Nov 2005 21:50:55 GMT
In terms of enhancements, maybe not a biggie but something I often think about when coding... there's what I think's a nice feature in a certain other language.
OK, the scenario is to do with input validation on forms, and has to do with this type of situation (this is a bit simplistic, but illustrates my point)
We have this code...
txtBox1_click(textbox: TextBox input) updating;
vars
begin
if textbox.text.trimBlanks = "" then
// probably inform user, beep or whatever here
textbox.setFocus;
endif;
end;
And of course the problem is, what say the user clicked a button that is say "Undo" or "Cancel" where we don't want that validation to occur and in fact they are now locked into the textbox. OK, obviously we would use some more sophisticated methodology, but in my experience it tends to get messy and now to my suggestion that I think is a nice way of handling this type of situation...
- Controls have a "causesValidation" property which is by default set to "true"
- They also have a "validate" method which would be used instead of the "lostFocus" method, e.g.
txtBox1_validate(cancel : Boolean output)
varsbegin
if txtBox1.text.trimBlanks = "" then
// inform the user or whateever)
cancel := true; // this causes focus to be returned to this textbox.
else
cancel := false;
endif;
end;
Now, this validate method is only invoked when another control with the "causesValdation" property set to "true" gains focus (remembering all controls by default are populated with this value), so during form design we change that property to "false" for our "Undo" button.
Yes, I do understand there are lots of ways to handle this, but I always liked this particular feature. Also, yes, in general I prefer doing all validation upon final update of form but sometimes there are needs to handle it during a users input.
Curious any thoughts about this?
Regards
Dave Mellow
In terms of enhancements, maybe not a biggie but something I often think about when coding... there's what I think's a nice feature in a certain other language.
OK, the scenario is to do with input validation on forms, and has to do with this type of situation (this is a bit simplistic, but illustrates my point)
We have this code...
txtBox1_click(textbox: TextBox input) updating;
vars
begin
if textbox.text.trimBlanks = "" then
// probably inform user, beep or whatever here
textbox.setFocus;
endif;
end;
And of course the problem is, what say the user clicked a button that is say "Undo" or "Cancel" where we don't want that validation to occur and in fact they are now locked into the textbox. OK, obviously we would use some more sophisticated methodology, but in my experience it tends to get messy and now to my suggestion that I think is a nice way of handling this type of situation...
- Controls have a "causesValidation" property which is by default set to "true"
- They also have a "validate" method which would be used instead of the "lostFocus" method, e.g.
txtBox1_validate(cancel : Boolean output)
varsbegin
if txtBox1.text.trimBlanks = "" then
// inform the user or whateever)
cancel := true; // this causes focus to be returned to this textbox.
else
cancel := false;
endif;
end;
Now, this validate method is only invoked when another control with the "causesValdation" property set to "true" gains focus (remembering all controls by default are populated with this value), so during form design we change that property to "false" for our "Undo" button.
Yes, I do understand there are lots of ways to handle this, but I always liked this particular feature. Also, yes, in general I prefer doing all validation upon final update of form but sometimes there are needs to handle it during a users input.
Curious any thoughts about this?
Regards
Dave Mellow