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

时间:2008-11-20 19:59:12

标签: oop asp-classic

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

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

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

我不喜欢我为每个调用验证类的对象需要一个错误成员变量。关于更好设置的建议?

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

4 个答案:

答案 0 :(得分:2)

你应该尝试ajaxed中使用的验证概念(这是一个适用于经典ASP的AJAX库 - www.webdevbros.net/ajaxed/)。不幸的是,验证器将在版本2.0中正式发布,但它已在SVN中提供 - 您可以在没有整个库的情况下轻松使用它(独立)

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

示例:

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

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

答案 1 :(得分:0)

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

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

您还可以将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

如果您想控制它,可以输出错误信息。

我邀请SO老师提出这方面的任何缺点。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我用几种不同的方式编写了自己的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
    // ...

}

然后,您可以从任何需要的地方调用该方法(例如,在验证表单输入时)。

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

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