views:

104

answers:

1

Hi

I have a validation class that implements IDataErrorInfo that has all the methods that you must implement.

One is

 public string this[string columnName]
        {
            get 
            {
            }
        }
       return "";

Now in this switch statement I have some validation for my form.

Now I noticed lots of my forms need the exact same validation stuff.

So now when I need to add more validation I make a new class that implements "IDataErrorInfo " add the extra valdation.

Then in the view I do something like this

CoreValidation coreReg = new CoreValidation();
 try
            {
                UpdateModel(coreReg, regForm.ToValueProvider());

            }
            catch
            {

                return View();
            }

            MoreValidation moreReg = new MoreValidation();

            try
            {
                UpdateModel(moreReg , regForm.ToValueProvider());

            }
            catch
            {

                return View();
            }

I just don't like that fact I need to have 2 separate try catch statements that could keep growing. If I needed to say take "core" and "More" and add some more validation that can't go into either of these I am looking at another try catch.

I tried to go to the "CoreValidation" class and extract out the switch statement and the Properties and put it in a new class.

My plan was then to just call that class and the method with the swtich statement. So in the "More" validation class I would in this

public string this[string columnName]
        {
            get 
            {
               CoreValidtion(columnName); // this would contain the switch statement

              //add another switch here with extra validation.
            }
        }
       return "";

Then I would only have one try catch and I would not be duplicating any code since everything would call the same method(CoreValidation).

But the updateModel can't seem to figure this out. When the UpdateModel tries to setup whatever it needs to setup it can't find the property methods so all the properties like

"UserName" are all set to null.

So how can I do what I want to do?

Thanks

A: 

I would suggest you take a look at a couple of sample asp.net mvc project , especially nerdinner and contactmangement example to see how validation is done in those examples. Those examples will give you a very clear guide on validation.

I had a post to list those sample projects here.

J.W.