Order of evaluation of Data Annotations in .Net Core

net 6 newbie 121 Reputation points
2022-10-06T05:50:40.067+00:00

Is there any way we can control the order of evaluation of the Data Annotations in .Net Core ?

public class Model1  
{  
       [Required]  
       [CustomAttribute1]  
       [CustomAttribute2]  
       public string data1 {get; set;}  
}  

In this model I want the following sequence of validation
Required ->CustomAttribute1->CustomAttribute2

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2022-10-06T08:14:08.587+00:00

    Hi @net 6 newbie ,

    I want the following sequence of validation
    Required ->CustomAttribute1->CustomAttribute2

    You can create one custom validation attribute which contains three validation rules.

    For example:

     public class Model1  
     {   
            [CustomAttribute]   
            public string data1 {get; set;}  
     }  
    

    In the custom attribute, you can add validation rules, and if you want to display error messages one by one, you can return validation results in the "if" statement. If you want to display all error messages at once, you can define a variable to collect the error messages and then return the error messages after the validation rule is complete.

     public class CustomAttribute : ValidationAttribute  
     {  
         protected override ValidationResult IsValid(object value, ValidationContext validationContext)  
         {  
             //get the entered value  
             var model= (Model1)validationContext.ObjectInstance;  
    
             //define a variable to collect the error message, after that display them at once.  
             //var errormessage ="";  
    
             //Check if the data is null or empty  
             if (String.IsNullOrEmpty(model.data1))  
             {   
                 //if data is null.  
                 return new ValidationResult(ErrorMessage == null ? "Value is required" : ErrorMessage);   
             }  
             //CustomAttribute1 validation rules  
             //if(condition){}  
      
             //CustomAttribute2 validation rules  
             //if(condition){}  
    
            //If you want to display all the validation message at once, use an `if` statement to check whether the errormessage is null, then return the validation result.  
    
             return  ValidationResult.Success;  
         }  
     }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    1 person found this answer helpful.

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-10-07T15:55:18.933+00:00

    just make a custom order by validation attribute.

    [Required]
    [OrderBy(first=Custom1, second=Custom2, third=Custom3)]

    required is always first.

    note: you will lose client validation, the client validation is in adapter order, rather than attribute.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.