Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DataAnnotations 4.0

I am using DA 4.0 with a MVC application and have created a custom validator as shown below:

public static ValidationResult NumberOfItems(int numItems, ValidationContext pValidationContext)
{
    if (numItems == 1)
    {
        //Tag as critical error
        //return new ValidationResult... 
    }

    if (numItems > 1 && numItems <= 10)
    {
        //Tag as non critical error
    }

    //Else it's successful
    return ValidationResult.Success;
}

I'd like to tag an error message as a Critical error or not. If it's not a critical error, I'd like to access this in my view and render it in a different way.

So, there are 2 parts to this:

  1. Tag failures as different types in the custom validator
  2. Modify the default ModelBinder to identify the critical error

How would I do this?

like image 440
Dave Schilling Avatar asked Nov 06 '22 12:11

Dave Schilling


1 Answers

Both of your questions require re-writing a whole bunch of MVC's internal error handling code. There is no easy path that I can see to add Error severity to all the different places ModelState and ViewModel validation occur.

The only answer to "How would I do this?" is "with a lot of custom code". ;)

like image 123
John Farrell Avatar answered Nov 15 '22 07:11

John Farrell