Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Need assistance with 'field validation'
Message
De
03/03/2003 17:00:51
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
À
03/03/2003 09:52:07
Mike Sue-Ping
Cambridge, Ontario, Canada
Information générale
Forum:
Visual FoxPro
Catégorie:
Base de données, Tables, Vues, Index et syntaxe SQL
Divers
Thread ID:
00759567
Message ID:
00760558
Vues:
24
>Hilmar,
>
>I am definitely interested in seeing how you do your validation code and would greatly appreciate any that you care to share. The stuff about corresponding error messages is what I'm now curious about too.

I include my validation function at the end of this message.

The table Validation.dbf contains following fields:

Table C(20) (name of table to be validated)
Rule M (rule that has to evaluate to .T.)
Message M (Error message if rule evaluates to .F.)
Level N(1) (0=ignore rule; 1=warning only; 2=error message-can't save)

Before saving data in a form (in Visual Extend, this is method Form.Valid()), I call this function, for instance:
if RecordValid("Client") && validation rules for table "Client"
  * proceed with save
endif
The function itself is saved in stored procedures.

HTH, Hilmar.
**********************************************************************
FUNCTION RecordValid(tcTableName)
	* Record validation.
	* Meant to be invoked from form.valid(<tablename>).
	* Can also be invoked from other places, for instance, triggers.

	llEvaluateAllRules = .T.
	tcTableName = lower(tcTableName)

	* 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 = 2 && Ignore
			loop
		endif
		select (lnOldAlias)
		if not evaluate(validation.rule)
			lcErrorText = lcErrorText + iif(validation.level = 1, "Warning: ", "Error: ") + Validation.Message
			if Validation.level = 0
				llError = .T.
			endif
			if llEvaluateAllRules
				lcErrorText = lcErrorText + chr(13)
			else
				exit
			endif
		endif
		select validation
	endscan

	* show error message
	if not empty(lcErrorText)
		MessageBox(iif(llError, "Can't save:", "Error saving:") + chr(13) + chr(13);
			+ lcErrorText + iif(llError, "", chr(13) + "Changes saved."),;
			16, iif(llError, "Error saving", "Warning saving"))
	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)
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform