[Reference] [Castle ar] 3. Validate

Source: Internet
Author: User
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 );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.