Validating Data

We've created elements, created a ruleset with some rules, and have captured a request from an html form. Our next step is to take a look at a few ways that we can perform validation on the elements, and also see how errors and validity are indicated within the component.

Validation against an entire ruleset

Many times when validating information from a form you simply want to determine whether all of the information is valid or not, this is easily accomplished with the Validate method of the ASPFormV1 class. In our example, we simply would call validate, if you have multiple rulesets configured ( something that won't be talking about in this example ), then you can either specify a ruleset or specify nothing and have all rulesets validate against the data. The example below shows how we would validate our example, note that validate returns whether the data is valid, this information is also stored in the IsValid property of the class.

Example 2.8. Validating With An Entire Ruleset

	'the validate method
	retVal = hASPForm.Validate
	
	'determining whether the data is valid through the isvalid property
	fIsValid = hASPForm.IsValid

Validating a single element

There are times where you might want to validate a single element instead of your entire element set. To facilitate this two different methods were created, one allows you to specify the element name to validate from the ASPFormV1's validate method, the other is using a validate method that we created for the element class, this method has an optional argument to allow you to specify a ruleset name ( multiple rulesets are not being utilized in this example, so we will just leave it blank ). Below is an example of how we might validate the name element.

Example 2.9. Validating The Name Element

	'using the ASPFormV1 validate method
	retVal = hASPForm.Validate( "", "name")
	
	fIsValid = hASPForm.Elements("name").IsValid
	
	'using the Element's validate method	
	retVal = hASPForm.Elements("name").Validate
	
	fIsValid = hASPForm.Elements("name").IsValid

The IsValid And IsDirty Flags

As seen above the IsValid property is available in both the ASPForm class and the Element class. IsValid returns false until it is validated, that way you don't have to worry about forgetting to validate your elements and the thing returning true, that would be bad.

The IsDirty flag is another property that should be mentioned at this time, the IsDirty flag simply indicates whether the value of an element has changed since the last the element was validated, it works like you would expect, simply returning a boolean value.

A Note About The Errors Object

There is another way to look at the results of a validation and that is through the use of the Errors object. We mention it here for completeness, but we will defer discussion to the next session.