Multi field validator using Enterprise Library - Validation Application block
There are number of built-in ASP.NET validation controls(RequiredFieldValidator, CompareValidator, CustomValidator and few more) exist. But there is no multi field validator. It is quite feasible to write custom validator to meet the requirement and there are third party controls available such as this one.
I was looking for a Required Field validator that checks all the controls(TextBoxes, Drop down lists and Listboxe controls) on the form and outputs appropriate message when atleast one of the controls is not used/selected. I want to stop the search if the end user has not entered any text in TextBox / selected Dropdownlist / List Box controls.
Validation Application block from enterprise library is catering more than what I was looking for. Above requirement is easily met with just few lines of code(shown below)I found it really interesting and easy to use Validation Application block from Enterprise Library as explained below.
Enterprise Library - Validation Application Block
Below custom validator class that can be in your App_Code folder or your desired location.
Step 1
Declare properties in a class with required Validation rules
//Required name spaces for validation block using Microsoft.Practices.EnterpriseLibrary.Validation; using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; /// <summary> /// Declare fields and properties with ///desired validation rules /// </summary> public class EmployeeValidator { //Private Members private string FirstName = string.Empty; private string LastName = string.Empty; private string Country = string.Empty; //Properties with validation rules [StringLengthValidator(1, 10, MessageTemplate= "Please enter your search criteria")] public string _FirstName { get { return FirstName; } set { FirstName = value; } } [StringLengthValidator(1, 10, MessageTemplate = "Please enter your search criteria")] public string _LastName { get { return LastName; } set { LastName = value; } } [StringLengthValidator(1, 10, MessageTemplate = "Please enter your search criteria")] public string _Country { get { return Country; } set { Country = value; } } }
Step 2
Implement your validation rules by creating an instance of your validation object and associating your properties with ASP.NET Controls input as shown below.
protected void btnSearch_Click(object sender, EventArgs e) { //Message lable lblValidationMsg.Text = ""; //Create an instance of Custom validator class EmployeeValidator Employee = new EmployeeValidator(); //Assign form controls text to your business object - properties Employee._FirstName = txtFirstName.Text; Employee._LastName = txtLastName.Text; Employee._Country = ddlCountry.SelectedValue; //Create an instance of Validator and associate //to your objects Validator<EmployeeValidator> validator = ValidationFactory.
CreateValidator<EmployeeValidator>(); ValidationResults results = validator.Validate(Employee); // Check if valid if (!results.IsValid) { if (results.Count < 3) { //your button_click event code goes here } else { // If not valid, loop through the ValidationResults foreach (var result in results) { lblValidationMsg.Text = result.Message; } } } }
Above few lines of code do the rest for you. You are ready to take off!
References