Topic: Is there some way I can get failed validators on a field? |
Posted On: 10/1/2008 11:39 PM
Posted By: Ryan Aljets
|
I have defined multiple validation rules in the designer and that works great. If the validation fails I can use the ErrorString property to get all the error message. But I would like to get access to all the validators that failed. Is that possible?
Ryan
Ryan, each field has a Validators property that gives you access to all the validators. The following code shows how you can check if a Validator is valid or not and also do anything further with the validator if required.
| foreach (BaseFieldValidator validator in customer.FirstName.Validators) | | { | | if (validator is RequiredFieldValidator) | | { | | RequiredFieldValidator rf = validator as RequiredFieldValidator; | | // do something with RequiredFieldValidator | | // the same process can be used for other types of validators as well. | | } | | if (!validator.IsValid) | | { | | // this is a failed validator do something here. | | } | | } | | |
Hope that helps you. If you need any further information/clarification let us know.
Thanks, Ish
| 10/2/2008 2:39:30 AM Ish Singh
|
Awesome! I was going to ask you another question but while typing the question I think I got my answer but you can tell me if I am wrong.
I need to get all the failed validators for all the fields in my object so I am thinking to write a loop that will iterate over the Fields collection of the object. Then on each field I can access the Validators collection.
I will try this tomorrow and let you know if I hit a snag.
Ryan
P.S. Really appreciate your fast and helpful responses!
| 10/2/2008 6:15:33 AM Ryan Aljets
|