Solved

Input Validator

Posted on 2013-05-22
13
231 Views
Last Modified: 2013-05-30
Hello Experts,

I am developing  C# winform application on .NET 4.x framework. I am tired of writing validation in form level as business process keeps changing. What I am looking at one class for each form where in I can write all validations for each controls and just call the validation class to validate all controls with one single error provider. May be an user control for the class.

Not sure how to start off itself :(

Any suggestions ? Please dont suggest application validation block, fluent etc as I have already gone through those and felt it is better write my own validation class
0
Comment
Question by:GRChandrashekar
  • 7
  • 6
13 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39186792
This might be silly but how can you put ALL the validation code for every possible point of entry into one class ?  (A 10 digit string is different than a date than a currency..... and you say the requirements change).


Maybe you might want to look at developing your own self-validating controls.  For instance you could code a TextBox (based on the standard TextBox) that will only accept integers in a certain range (you supply the range at run time).
0
 

Author Comment

by:GRChandrashekar
ID: 39186996
why not develop my own class?

Say i have tbxname, i write a class pass tbxname input to class and in class define whether it should be null or not.

I can pass dteventdate to class and in class define validation for that.

similarly for all contorls. but am not sure how to start off
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187136
I think that is a bad idea or are you talking of just validating the input from one form.
0
 

Author Comment

by:GRChandrashekar
ID: 39187743
Nopes I am talking of validation of all forms and each form in diff class. I do agree it may be bad idea but not feasible to write huge code in each form
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187793
>>and each form in diff class

OK - in your original question it sounded like just one class for all forms.  That is why I criticised it at that point.

What you propose is a rather better design.


I would suggest your validation class would have one function per control, which takes the current contents of that control (not the control itself - separation of logic from GUI) and performs the necessary validation.  Return could be a string with the error message or an empty string if the validation succeeds.
0
 

Author Comment

by:GRChandrashekar
ID: 39187839
Exactly. So what I am requesting you experts is just one example to validate a text box, a list box, a radio button and a combo box so that I can understand how to go about and complete the design which i intent to
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187952
public String MyTextValidator(String inp)
{
  if(inp.Length > 10)
    return "To many chars";
  else
    return "";
}

and so on.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39187959
In the form in some function to check the inputs of all controls:

String s = x.MyTextValidator(txt1.Text);
if(s.Length > 0)
  {
    //Oops - wrong input
    MsgBox.Show(s);
    return false;
   }
.....
//All is OK
return true;
0
 

Author Comment

by:GRChandrashekar
ID: 39188084
Apologies what I would prefer is loop through all controls in class level by passing parent.control from form to class and write validation for each control in the class. in form level it should just be like validatecontrols(this) which should get true or false from class
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 39188098
No problems.  Just loop through the controls in the form
something like
foreach(control ctl in this.Controls)
{
  ValidateThisControl(ctl);
}
0
 

Author Comment

by:GRChandrashekar
ID: 39188151
foreach(control ctl in this.Controls)
{
  ValidateThisControl(ctl);
}

This I guess in form level !

What I need is to pass the form to class, in class loop through controls.
0
 

Author Comment

by:GRChandrashekar
ID: 39188209
Now some how I managed like this.
 public static void ClearControls(Control parent)
        {
            foreach (Control parentctrl in parent.Controls)
            {
                if (ReferenceEquals(parentctrl.GetType(), typeof (GroupBox)))
                {
                    foreach (Control parentgbxctrl in parentctrl.Controls)
                    {
                        if (parentgbxctrl.Visible)
                        {
                            if (ReferenceEquals(parentgbxctrl.GetType(), typeof (TextBox)))
                            {
                                if (parentgbxctrl.Name == "tbxdescription")
                                {
                                    if (ValidateRuleClass.Isrequiredstring(parentgbxctrl.Text))
                                    {
                                       if (ValidateRuleClass.Isalphastring(parentgbxctrl.Text))
                                       {
                                           
                                       }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

This suits my purpose. Now I need to return false after line if (ValidateRuleClass.Isrequiredstring(parentgbxctrl.Text)) if validation fails and again false after line if (ValidateRuleClass.Isalphastring(parentgbxctrl.Text)) if validation fails. if any one fails i have to return false to form. if both is true i have to return to true to form.
now if the result if false, i need to show error provider in form ( i have only one error provider} with error message for each control if there are multiples. How can I do this? This will solve my problem
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 39188860
Like I said earlier - handle the controls in the form and call separate functions to validate the contents of the control.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now