Enterprise Library-validation Application Block learning Manual (Latest Version) Part 6

Source: Internet
Author: User
This article demonstrates the use of the Enterprise Library-validation Application Block Verification Management Module. Exercise creating multiple rule sets and using different validation rules for data verification in different scenarios. This article by the http://blog.entlib.com open source ASP. Net blog platform team according to the entlib Hol manual compilation to provide, welcome to exchange. Exercise 6: create and use multiple rule setsThe following operations can be performed on the basis of Exercise 5, or directly open the validationhol. sln project file under the \ Enterprise Library 4.1 Hol \ CS \ validation \ labs \ lab06 \ before directory. The solution includes two projects: validationhol. businesslogic and validationhol. 1. create and configure a new, non-default rule set (1) Use entlib's configuration management tool to open the app. config file, right-click the customer node, select new | rule set, and create a new rule set named alternative. (2) Select the alternative node. Just like the previous operation, only two attributes are added, firstname and lastname, respectively. Set the relevant attributes of String Length validator of firstname: lowerbound = 1 lowerboundtype = inclusiveupperbound = 20. Continue to set the domainvalidator attribute of lastname, select domain attribute, and click the corresponding button, the domainvalue collection editor dialog box is displayed.
In this dialog box, add entlib, Smith, Jones, Doe, and other domainvalues respectively, so that the specified property value must be entered, as shown in. Remember to save the configuration file after the operation is complete. 2. Add a checkbox to the UI to select which rule set to use to open the mainform interface and add a new checkbox. Set the following attributes: Name = alternativevalidationtext = alternative, as shown in.
3. Update the acceptbutton_click method in the mainform class. the checkbox determines which rule set is used to verify the customer object instance. The Code is as follows. Private void acceptbutton_click (Object sender, eventargs e) {customer = new customer {firstname = firstnametextbox. text, lastname = lastnametextbox. text, SSN = ssntextbox. text, address = new address {streetaddress = streetaddresstextbox. text, city = citytextbox. text, state = statecombobox. text, zipcode = zipcodetextbox. text }}; validationresults Results = This. alternativevalidation. ch Ecked? Validation. Validate (customer, "alternative "): Validation. Validate (customer); If (! Results. isvalid) {stringbuilder builder = new stringbuilder (); builder. appendline ("customer is not valid:"); foreach (validationresult result in results) {builder. appendline (string. format (cultureinfo. currentculture, "{0 }:{ 1}", result. key, result. message);} MessageBox. show (this, builder. tostring (), "error", messageboxbuttons. OK, messageboxicon. error); return;} MessageBox. show (this, "proces Sing customer' "+ customer. firstname + "'", "working", messageboxbuttons. OK, messageboxicon. information);} 4. run the sample program and check whether different rule sets are used. If no checkbox is selected, the default rule set defined in the previous section is used to verify the customer object instance, the result is the same as that in the previous section. If you select alternative checkbox, the new alternative rule set is used in this section. The message dialog box is displayed, as shown in.
Http://www.entlib.com professional ASP. NET e-commerce platform team, welcome to continue to visit the validation Application Block learning manual. Reference:Validation Application Block hands-on labs for Enterprise Library

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.