Creating Rulesets And Rules

Loading post data, or grabbing information from a session isn't terribly useful by itself, in fact it would be easier to write code by hand to load this information every time. All data that is posted via a web interface should be validated for length, type, format, among others, with the ability to turn off javascript in the newer browsing clients, validation at the client is not enough, it must be done on the server as well.

ASPForms has a sophisticated mechanism for managing the validation of data. For now, we will cover the basic set of the components features, and in a later chapter we will cover some more advanced features of the system. Below we will construct a ruleset for the form, adding rules to validate all of the elements in our form.

Example 2.6. Configuring a validation ruleset

	'create an ASPForms object
	set hASPForm = Server.CreateObject("ASPForms.ASPFormV1")
	
	
	'*** add some elements to the form ***
	
	'create a name element in the form
	hASPForm.Elements.CreateElement "name", true
	
	'create an address1 element in the form
	hASPForm.Elements.CreateElement "address1", true
		
	'create an address2 element in the form
	hASPForm.Elements.CreateElement "address2", false
	
	'create a city element in the form
	hASPForm.Elements.CreateElement "city", true				
	
	'create a state element in the form
	hASPForm.Elements.CreateElement "state", true				
	
	'create a zipcode element in the form				
	hASPForm.Elements.CreateElement "zipcode", true
	
	'store the form's state and configuration into the session
	hASPForm.StoreStateInSession "addressInfo"


	'*** create a ruleset and some rules for validation ***
	
	'create a new ruleset for the form
	hASPForm.Rulesets.CreateRuleset("master")
	
	'add a maximum length rule for the name element	
	hASPForm.Rulesets("master").CreateRule "name_maxlength", "name", ASPFORMS_MAXLength, "The Name is Too Long", 15
	
	'add a rule to only allow the characters A-Z, a-z, 0-9 and spaces in address1
	hASPForms.Rulesets("master").CreateRule "address2_allowablechars", "address1",  ASPFORMS_RegularExpression, "Only A-Z, a-z, and 0-9", "^[A-Za-z0-9 ]+"

Looking at the CreateRule Method used above you will notice that there are several arguments passed in, the function breaks down like this :

Example 2.7. The CreateRule Method

	'the create rule method	
	ASPFormV1.Rulesets([rulesetname]).CreateRule [rulename], [elementname], [ruletype], [error_string], [param1]

	[rulesetname] - name of the ruleset that this rule should apply to
	[rulename] - name for this rule, ever rule in a ruleset must have a unique name
	[elementname] - name of the element that this rule applies to
	[ruletype] - there are several ruletypes that are available within aspforms we will cover these in a bit
	[error_string] - the string that should appear as the description of an error object if the rule conditions are not met
	[param1] - a parameter that has a different meaning for different ruletypes ( see api documentation for details )