关于如何实现验证类的建议?

时间:2022-07-29 01:55:15

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

我正在实现经典ASP中的验证类。验证类应该如何与我的其他类接口?

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:

我当前的设置:User类的set方法在验证类中调用相应的验证方法。发生的任何错误都存储在User.mError中。例如,这是我在ASP Classic中的Email成员变量的set方法:

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?

有关验证架构的任何建议我应该作为基准进行审核吗?

4 个解决方案

#1


2  

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中使用的验证概念(这是经典ASP的AJAX库 - www.webdevbros.net/ajaxed/)。不幸的是,验证器将在版本2.0中正式发布,但它已经在SVN中可用 - 您可以在没有整个库的情况下轻松使用它(独立)

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.

Ajaxed有一个名为validator的类,您可以使用它来验证业务对象。它需要创建一个isValid()方法,该方法将Validator作为参数,并在实例有效或无效时返回。在保存实例之前调用isValid()方法。如果任何内容无效,它将执行所有验证并填充给定的验证器。

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.

我已经使用这种方法多年了,它非常灵活。您可以将Validator类作为独立类使用,但我建议您将ajaxed库作为一个整体使用。它可以让您更轻松地开发ASP。

#2


0  

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

我建议查看.net框架提供的Validator相关类。

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

在你的情况下,你可以有一个Validator类(特定的EmailValidator),它可以有一个名为Validate的方法,它接受一个字符串,返回一个布尔值

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

您还可以将ErrorMessage作为Validate函数的参数之一传递,例如


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.

我邀请SO家伙提出这方面的任何缺点。

#3


0  

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

Spring有自己的验证器模式,用于验证复杂对象并返回多个错误。它在这里详述。

#4


0  

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:

我用几种不同的方式编写了自己的Validator类。本质上的验证不一定需要实例化对象,因此我创建了一个使用静态方法进行验证的类。我使用了一种验证方法,你必须传递一个类型(例如电子邮件,名字,网站......),或者每种方法都有一个类型。最后,我真的只需要一种算法,所以我选择了一种方法。实质上,有类属性可以保存每种类型的验证正则表达式,以及给定类型的关联错误消息。这一切都等同于类似下面的类:

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.

我道歉上面是用PHP编写的,问题是关于ASP,但你得到了我的漂移。

#1


2  

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中使用的验证概念(这是经典ASP的AJAX库 - www.webdevbros.net/ajaxed/)。不幸的是,验证器将在版本2.0中正式发布,但它已经在SVN中可用 - 您可以在没有整个库的情况下轻松使用它(独立)

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.

Ajaxed有一个名为validator的类,您可以使用它来验证业务对象。它需要创建一个isValid()方法,该方法将Validator作为参数,并在实例有效或无效时返回。在保存实例之前调用isValid()方法。如果任何内容无效,它将执行所有验证并填充给定的验证器。

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.

我已经使用这种方法多年了,它非常灵活。您可以将Validator类作为独立类使用,但我建议您将ajaxed库作为一个整体使用。它可以让您更轻松地开发ASP。

#2


0  

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

我建议查看.net框架提供的Validator相关类。

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

在你的情况下,你可以有一个Validator类(特定的EmailValidator),它可以有一个名为Validate的方法,它接受一个字符串,返回一个布尔值

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

您还可以将ErrorMessage作为Validate函数的参数之一传递,例如


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.

我邀请SO家伙提出这方面的任何缺点。

#3


0  

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

Spring有自己的验证器模式,用于验证复杂对象并返回多个错误。它在这里详述。

#4


0  

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:

我用几种不同的方式编写了自己的Validator类。本质上的验证不一定需要实例化对象,因此我创建了一个使用静态方法进行验证的类。我使用了一种验证方法,你必须传递一个类型(例如电子邮件,名字,网站......),或者每种方法都有一个类型。最后,我真的只需要一种算法,所以我选择了一种方法。实质上,有类属性可以保存每种类型的验证正则表达式,以及给定类型的关联错误消息。这一切都等同于类似下面的类:

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.

我道歉上面是用PHP编写的,问题是关于ASP,但你得到了我的漂移。