Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom validation attribute that compares the value of my property with another property's value in my model class

I want to create a custom validation attribute, in which I want to compare the value of my property with another property's value in my model class. For example I have in my model class:

...     public string SourceCity { get; set; } public string DestinationCity { get; set; } 

And I want to create a custom attribute to use it like this:

[Custom("SourceCity", ErrorMessage = "the source and destination should not be equal")] public string DestinationCity { get; set; } //this wil lcompare SourceCity with DestinationCity 

How can I get there?

like image 387
TheForbidden Avatar asked Aug 14 '12 19:08

TheForbidden


People also ask

How do I create a custom validation Attribute?

To create a custom validation attributeIn Solution Explorer, right-click the App_Code folder, and then click Add New Item. Under Add New Item, click Class. In the Name box, enter the name of the custom validation attribute class. You can use any name that is not already being used.

What are different attributes available in model for validation?

See [Required] attribute for details about this attribute's behavior. [StringLength]: Validates that a string property value doesn't exceed a specified length limit. [Url]: Validates that the property has a URL format. [Remote]: Validates input on the client by calling an action method on the server.

What does custom validator do?

Use the CustomValidator control to provide a user-defined validation function for an input control. The CustomValidator control is a separate control from the input control it validates, which allows you to control where the validation message is displayed. Validation controls always perform validation on the server.


1 Answers

Here's how you could obtain the other property value:

public class CustomAttribute : ValidationAttribute {     private readonly string _other;     public CustomAttribute(string other)     {         _other = other;     }      protected override ValidationResult IsValid(object value, ValidationContext validationContext)     {         var property = validationContext.ObjectType.GetProperty(_other);         if (property == null)         {             return new ValidationResult(                 string.Format("Unknown property: {0}", _other)             );         }         var otherValue = property.GetValue(validationContext.ObjectInstance, null);          // at this stage you have "value" and "otherValue" pointing         // to the value of the property on which this attribute         // is applied and the value of the other property respectively         // => you could do some checks         if (!object.Equals(value, otherValue))         {             // here we are verifying whether the 2 values are equal             // but you could do any custom validation you like             return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));         }         return null;     } } 
like image 71
Darin Dimitrov Avatar answered Sep 23 '22 06:09

Darin Dimitrov