Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you determine which validator failed?

I am working with a page and I am getting an Page.IsValid = false and I am trying to determine which control is causing the validation problem.

like image 432
Robert Avatar asked Aug 01 '11 15:08

Robert


People also ask

What is validation failure?

Failed Validation means that the data in at least one field in the record does not match the required formatting or data reference validations for that field.

What is validator error?

A validation error occurs when you have validation/response checking turned on for one of the questions and the respondent fails to answer the question correctly (for numeric formatting , required response).


2 Answers

Credit to Steven for this answer, but I had to make some changes for it to work as this.Validators.Where() had some problems.

using System.Linq;  List<IValidator> errored = this.Validators.Cast<IValidator>().Where(v => !v.IsValid).ToList(); 
like image 198
Ozziereturnz Avatar answered Sep 30 '22 13:09

Ozziereturnz


In code (page_load), you can do this:
(per MSDN: http://msdn.microsoft.com/en-US/library/dh9ad08f%28v=VS.80%29.aspx)

If (Me.IsPostBack) Then     Me.Validate()     If (Not Me.IsValid) Then         Dim msg As String         ' Loop through all validation controls to see which          ' generated the error(s).         Dim oValidator As IValidator         For Each oValidator In Validators             If oValidator.IsValid = False Then                 msg = msg & "<br />" & oValidator.ErrorMessage             End If         Next         Label1.Text = msg     End If End If 

In the markup, you can...

  • You can put "text" on your validator (like an asterisk...)
  • Or use a validation_summary control (which requires an error message on your validator)...
like image 43
Chains Avatar answered Sep 30 '22 13:09

Chains