Server side validations and unit testing in MVP We are following passive controller approach and when user clicks on submit, server side validations are fired. There are many other fields on the screen that needs to be validated.

I would like to get feedback about the following approach.


Is it ok to have validations in Presenter.
As there are many fields in UI, PerformServerValidations() method is getting big. Is there any way I could refactor it.
Intention behind declaring PerformServerValidations() as public and returning a IList is to be able to test it. Is it a good approach to take? 

[TestMethod]
public void Presenter_PerformServerValidations_ValidateFromCustomerId_ExpectFalseForInvalidNumber()
{
    //creating presenter
    presenter.View.Stub(x=&gt;x.FromCustomerId).Return("one two three");    

    var errorCodes = presenter.PerformServerValidations();

    Assert.IsTrue(errorCodes.Contains("ERR_InvalidFromCustomerId"));
}



Presenter.cs

public void OnSubmit()
{
    var serverValidationErrors = PerformServerValidations();
    this.View.ServerErrors = serverValidationErrors ; // View.ServerErrors will loop over the list and sets validator's IsValid property to false
    if(!serverValidationErrors.Any())
    {
         BindCustomers();
    }
}

public IList&lt;string&gt; PerformServerValidations()
{
   var errorCodes = new List&lt;string&gt;();

   int parsedFromCustomerId;
   int parsedToCustomerId;

   bool isValidFromCustomerId = int.TryParse(this.View.FromCustomerId, out 
parsedFromCustomerId);
   bool isValidToCustomerId = int.TryParse(this.View.ToCustomerId, out parsedToCustomerId);

   if(!string.IsNullOrWhiteSpace(this.View.FromCustomerId) &amp;&amp; !isValidFromCustomerId)
   { 
       errorCodes.Add("ERR_InvalidFromCustomerId");
   }

   if(!string.IsNullOrWhiteSpace(this.View.ToCustomerId) &amp;&amp; !isValidToCustomerId)
   { 
       errorCodes.Add("ERR_InvalidToCustomerId");
   }

   if((!string.IsNullOrWhiteSpace(this.View.FromCustomerId) &amp;&amp; !string.IsNullOrWhiteSpace(this.View.FromCustomerId) &amp;&amp; (isValidFromCustomerId &amp;&amp; isValidFromCustomerId))
   { 
       if(parsedFromCustomerId &gt; parsedFromCustomerId)
       {
           errorCodes.Add("ERR_InvalidCustomerIdRange");
       }
   }

   //There are many other fields to be validated
   return errorCodes;
}
I think you have a typo in the last outside if statement, I assume you meant two different variables when you wrote this: 

if((!string.IsNullOrWhiteSpace(this.View.FromCustomerId) &amp;&amp; !string.IsNullOrWhiteSpace(this.View.FromCustomerId) &amp;&amp; (isValidFromCustomerId &amp;&amp; isValidFromCustomerId))
{ 
    if(parsedFromCustomerId &gt; parsedFromCustomerId)
    {
        errorCodes.Add("ERR_InvalidCustomerIdRange");
    }
}


parsedFromCustomerId will never be greater than parsedFromCustomerId

and the obvious one that won't cause a bug but probably won't give you the intended results is

 (isValidFromCustomerId &amp;&amp; isValidFromCustomerId)


I assume you mean to put a different boolean variable in there instead of the same one twice.

this code won't function, correctly.  

probably a Typo but you should proofread your code again just to make sure.