Http://www.rainsts.net/article.asp? Id = 485
In business design, data is often subject to definite format restrictions. We usually do this on the user input interface, but castle ar provides us with another alternative. This function is very useful when we cannot determine whether the class library or service caller will perform a format check. To implement this function, activerecordvalidationbase/activerecordvalidationbase <t> and some matching verification features are required.
Verification features
- Validatenotempty: The property cannot be empty, including string. Empty.
- Validateconfirmation: Verify that the two attributes must be equal, such as password verification.
- Validateemail: Verify the email format.
- Validatelength: the length of the attribute string must be equal to or within a certain length.
- Validateregexp: Customize Regular Expression verification.
- Validatecreditcard: verifies the format of a credit card number, including common types of credit cards, such as Visa and MasterCard.
- Validateisunique: the attribute value must be unique in the data table.
We can also inherit from abstractvalidationattribute to create our own verification features.
Usage
Change the type base class to activerecordvalidationbase, and then add the corresponding verification feature. Call isvalid () before database operations ().
Demo[Activerecord ("users")]
Public class user: activerecordvalidationbase <user>
{
Private int ID;
[Primarykey (primarykeytype. Identity)]
Public int ID
{
Get {return ID ;}
Set {id = value ;}
}
Private string name;
[Property (unique = true, notnull = true)]
[Validatenotempty ("the user name cannot be blank! ")]
Public string name
{
Get {return name ;}
Set {name = value ;}
}
Private string password;
[Property]
Public String Password
{
Get {return password ;}
Set {Password = value ;}
}
Private string password2;
[Validateconfirmation ("password")]
Public String password2
{
Get {return password2 ;}
Set {password2 = value ;}
}
Private string email;
[Property]
[Validateemail ("Incorrect email address format! ")]
Public String email
{
Get {return email ;}
Set {email = value ;}
}
Private string address;
[Property]
[Validatelength (5, 50, "the length must be 5 ~ 50! ")]
Public String address
{
Get {return address ;}
Set {address = value ;}
}
Private string postcode;
[Property]
[Validatelength (6, "the length must be 6! ")]
[Validateregexp ("[\ D] {6}")]
Public String postcode
{
Get {return postcode ;}
Set {postcode = value ;}
}
Private string creditcard;
[Property]
[Validatecreditcard (creditcardvalidator. cardType. Visa)]
Public String creditcard
{
Get {return creditcard ;}
Set {creditcard = value ;}
}
}
Public class artester
{
Public static void test ()
{
Activerecordstarter. initialize (assembly. getexecutingassembly (),
New xmlconfigurationsource ("Ar. xml "));
Activerecordstarter. dropschema ();
Activerecordstarter. createschema ();
User user = new user ();
User. Name = "Zs ";
User. Password = "PWD ";
User. Email = "ABC ";
User. Address = "X ";
User. postcode = "XX ";
User. creditcards = "12345 fdas ";
If (user. isvalid ())
{
User. Create ();
}
Else
{
Foreach (string s in user. validationerrormessages)
Console. writeline (s );
}
}
}