Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Should code for checks be on form or in store procedures
Message
From
27/10/2005 09:03:00
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9
OS:
Windows XP
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01062538
Message ID:
01062604
Views:
10
>I would like to know what is the best practice to check that data on an entry form is correct and what are the advantages and disadvantages of each?
>
>I have seen this type of checking in one of 3 places:
>
>1) At the point where the data is captured (for example in the valid event of say a text box.
>
>2) A number of check are done in a method on the form. This method is called before the tableupdate is issued.
>
>3) Checks are written into a stored procedure. This procedure is then called each time before the referential integrity is run for a trigger in the table.
>
>
>Thanks in advance!

I very much prefer to do the checking, not in every individual textbox, but right before the save. (The trigger is actually safer, but I didn't take the trouble to do that.)

Thus postponing the saving has several advantages:
  • You don't have trouble with validations that span more than one field (e.g. "either debit or credit must have a value, but not both").
  • You don't have trouble with buttons on the form (the user can't access the "Cancel" button, because a TextBox.Valid() won't allow it).
  • The user can input data in any order.
  • Validating for each TextBox isn't safe anyway, in that the user can skip over individual TextBoxes with the mouse.

    So, I suggest to do all validation just before the TableUpdate(). I also suggest to put validation rules into a table. Here is my validation function for this purpose. I suppose it can also be adapted to be used from within the trigger.

    HTH,

    Hilmar.
    **********************************************************************
    FUNCTION RecordValid(tcTableName, llEvaluateAllRules)
    	* Record validation.
    	* Meant to be invoked from form.valid(tablename).
    	* Can also be invoked from other places, for instance, triggers.
    
    	tcTableName = lower(tcTableName)
    	llEvaluateAllRules = .T.  && Override second parameter.
    
    	* save and change settings
    	local lnOldAlias, lcOldExact
    	lnOldAlias = select()
    	lcOldExact = set("exact")
    	set exact off
    
    	* open validation table
    	if used("validation")
    		select validation
    	else
    		select 0
    		use validation again
    	endif
    	set order to "mainorder"
    
    	* evaluate all errors defined in rules table
    	local lcErrorText, llError
    	lcErrorText = ""
    	tcTableName = padr(tcTableName, len(Table))
    	seek tcTableName
    	scan while table = tcTableName
    		if Level = "I" && Ignore
    			loop
    		endif
    		select (lnOldAlias)
    		if not evaluate(validation.rule)
    			lcErrorText = lcErrorText + iif(Validation.Level = "W", "Advertencia: ", "Error: ") + Validation.Spanish
    			if not Validation.Level = "W"
    				llError = .T.
    			endif
    			if llEvaluateAllRules
    				lcErrorText = lcErrorText + chr(13)
    			else
    				exit
    			endif
    		endif
    		select validation
    	endscan
    
    	* show error message
    	if not empty(lcErrorText)
    		if llError
    			* MessageBox("Can't save"... "Error on save")
    			MessageBox("No se pudo guardar:" + chr(13) + chr(13) + lcErrorText,;
    				16, "Error al guardar")
    		else
    			llError = MessageBox("Warning on save:" + ... "save anyway?")
    			llError = MessageBox("Advertencia al guardar:" + chr(13) + chr(13) + lcErrorText;
    				+ chr(13) + "¿Quiere guardar de todos modos?",;
    				4 + 48 + 256, "Advertencia al guardar") # 6
    		endif
    	endif
    
    	* restore environment and return success status
    	use in validation
    	select (lnOldAlias)
    	set exact &lcOldExact
    	return not llError
    ENDFUNC
    Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)
  • Previous
    Next
    Reply
    Map
    View

    Click here to load this message in the networking platform