views:

257

answers:

4

I am implementing a validation class in classic ASP. How should the validation class interface with my other classes?

My current setup: The User class's set methods call the appropriate validation method in the validation class. Any errors that occur are stored in User.mError. For example, here's my set method for the Email member variable in ASP Classic:

Class User
  Property Let Email(EmailInput)
     If (myValidation.isEmail(EmailInput)) then
        mEmail = EmailInput
     Else
        mError = "Invalid Email Address format."
     End If

I don't like how I'm going to need an error member variable for every object that calls my validation class. Suggestions on a better setup?

Any suggestions for a validation architecture I should review as a benchmark?

A: 

I would suggest looking at Validator related classes provided by .net framework.

In your case, you can have a Validator Class (EmailValidator to be specific), which could have a method named Validate which takes a string, returns a boolean

You could also pass ErrorMessage as one of the parameters of the Validate function e.g.


Psuedo Code.

class EmailValidator
...
function Validate(byval EmailAddress as string, optional byval Result as string) as boolean)
..
if (condition success)
result = success
elseif (emailafddress doesnt have @)
result = "invalid email address. missing @"
endif
end function
end class

You can take error message out, if you want to have control over it.

I invite fellow SO guys to suggest any shortcomings in this.

shahkalpesh
A: 

Spring has its own validator pattern for validating sophisticated objects and returning multiple errors. It's detailed here.

Dan Vinton
A: 

I have written my own Validator class a few different ways. Validation in nature doesn't necessarily require an instantiated object, so I created a class with static methods to do validation. I have used one validator method where you have to pass a type in (e.g. Email, First Name, website...), or multiple methods each for the given types. In the end, there is really only one algorithm that I needed, so I went with the one method approach. Essentially, there are class properties to hold your validation regular expressions for each type, as well as an associated error message for the given type. It all equates to a class similar to the one below:

class Validation
{

    // define the properties for dealing with different type validations
    public static $firstNamePattern = '/[-a-zA-Z0-9._ ]{2,}/';
    public static $lastNamePattern = '/[-a-zA-Z0-9._ ]{2,}/';
    // ... more fields


    public static function validateText($type, $text, $fieldName)
    {
     $pattern = $type."Pattern";
     if ($this->$pattern != '')
     {
      // perfom the validation
            // ...
            return true; // or false
     }
    }

    // other validation methods below
    // ...

}

Then you can call that method from anywhere you need to (e.g. while validating form input).

if (Validation->validateText('firstName', $formFirstName, 'First Name'))
{
    // validation passed
}
else
{
    // validation failed
}

I apologize the above is written in PHP and the question was about ASP, but you get my drift.

localshred
+1  A: 

You should try the validation concept used in ajaxed (which is an AJAX library for classic ASP - www.webdevbros.net/ajaxed/). Unfortunately the validator will be officialy released in version 2.0 but its already available in SVN - you could easily use it without the whole library (standalone)

Ajaxed has a class called validator which you can use to validate your business objects. It requires the creation of an isValid() method which takes a Validator as an argument and returns if the instance is valid or not. The isValid() method is called before saving the instance. It performs all validations and fills the given validator if anything is invalid.

Example:

class User
 public firstname
 public lastname

 'validates the user instance
 '- call before save()
 public function isValid(byRef v)
  isValid = true
  if len(firstname) < 5 then
   v.add "firstname", "Firstname must be at least 5 chars long."
   isValid = false
  end if
  if len(lastname) < 5 then
   v.add "lastname", "Lastname must be at least 5 chars long."
   isValid = false
  end if
 end function

 public sub save()
  'do some DB stuff
 end sub
end class

'usage scenario 1 (simple - we just know if valid or not)
set u = new User
if u.isValid(new Validator) then
 u.save()
else
 response.write("User is invalid. some error happend")
end if

'usage scenario 2 (detailed - we have an error summary)
set u = new User
u.firstname = "Michal"
set v = new Validator
if u.isValid(v) then
 u.save()
else
 'the validator offers a helper to create a validation summary
 response.write(v.getErrorSummary("<div><ul>", "<ul/></div>", "<li>", "</li>"))
end if

'usage scenario 3 (we can even validator more users in one go)
set u1 = new User
set u2 = new User
set v = new Validator
u1.isValid(v)
u2.isValid(v)

if v then
 u1.save()
 u2.save()
else
 response.write("something is invalid")
end if

I am using this aproach for years already and its very flexible. You can use the Validator class as standalone but I would recommend you use the ajaxed library as a whole. It lets you develop ASP more easier.

Michal