Solved

Input Validator

Posted on 2013-05-22
13
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Winform Module - What is the ASP.Net equiv 2 40
defining NULL or 0 10 51
ASP.NET MVC -Update model when click on <li> element 29 70
C# XML Get Values 4 37
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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!
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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