2012-08-14 14 views
40

मैं एक कस्टम सत्यापन विशेषता बनाना चाहता हूं, जिसमें मैं अपनी संपत्ति के मूल्य की तुलना अपने मॉडल वर्ग में किसी अन्य संपत्ति के मूल्य के साथ करना चाहता हूं। उदाहरण के लिए मैं अपने मॉडल कक्षा में है:कस्टम सत्यापन विशेषता कैसे बनाएं?

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

और मैं एक कस्टम बनाना चाहते हैं इस तरह इसका इस्तेमाल करने का श्रेय:

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

मैं वहाँ कैसे मिल सकता है?

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; 
    } 
} 
+1

http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx – Joe

+1

@ जो, यह एएसपी.नेट एमवीसी 2 के लिए है और अब एमवीसी 3 पर लागू नहीं होता है। यह ब्लॉग भी पोस्ट यह नहीं बताता है कि वैधकर्ता में एक निर्भर संपत्ति मूल्य कैसे प्राप्त किया जाए, जो ओपी यहां हासिल करने की कोशिश कर रहा है। –

उत्तर

67

यहाँ कैसे आप अन्य संपत्ति के मूल्य प्राप्त कर सकते हैं है घटना हेन्डलर शामिल है।

internal sealed class CustomAttribute : Attribute 
    { 
     public CustomAttribute(string propertyName) 
     { 
      PropertyName = propertyName; 
     } 

     public string PropertyName { get; set; } 

     public string ErrorMessage { get; set; } 

     public static void ThrowIfNotEquals(object obj, PropertyChangedEventArgs eventArgs) 
     { 
      Type type = obj.GetType(); 
      var changedProperty = type.GetProperty(eventArgs.PropertyName); 
      var attribute = (CustomAttribute)changedProperty 
               .GetCustomAttributes(typeof(CustomAttribute), false) 
               .FirstOrDefault(); 

      var valueToCompare = type.GetProperty(attribute.PropertyName).GetValue(obj, null); 
      if (!valueToCompare.Equals(changedProperty.GetValue(obj, null))) 
       throw new Exception("the source and destination should not be equal"); 
     } 
    } 

प्रयोग

var test = new ModelClass(); 
    test.SourceCity = "1"; 
    // Everything is ok 
    test.DestinationCity = "1"; 
    // throws exception 
    test.DestinationCity ="2"; 

कोड आसान बनाने के लिए मैं एक मान्यता छोड़ करने का फैसला किया।

+0

बढ़िया, यह सिर्फ जवाब है ** मैं ** देख रहा हूँ! मेरे सत्यापन संदर्भ को छोड़कर हमेशा शून्य है। कोई विचार? –

+2

@GrimmTheOpiner मुझे पता है कि यह पुराना है, लेकिन किसी भी व्यक्ति के लिए 'सार्वजनिक ओवरराइड बूल RequiredValidationContext {कोशिश {सच वापसी; }} '' कस्टम एट्रिब्यूट 'में – Ryan

+0

@ रयान वाह, मैं ऐसा क्यों करना चाहता था? यहां तक ​​कि अगर मुझे याद हो सकता है, तो मैं अब दो नौकरियां कर रहा हूं! :-) –

4

मेरी उदाहरण के लिए नीचे की जांच करें:

मॉडल वर्ग औजार INotifyPropertyChanged

public class ModelClass : INotifyPropertyChanged 
     { 
      private string destinationCity; 
      public string SourceCity { get; set; } 

      public ModelClass() 
      { 
       PropertyChanged += CustomAttribute.ThrowIfNotEquals; 
      } 

      [Custom("SourceCity", ErrorMessage = "the source and destination should not be equal")] 
      public string DestinationCity 
      { 
       get 
       { 
        return this.destinationCity; 
       } 

       set 
       { 
        if (value != this.destinationCity) 
        { 
         this.destinationCity = value; 
         NotifyPropertyChanged("DestinationCity"); 
        } 
       } 
      } 

      public event PropertyChangedEventHandler PropertyChanged; 

      protected virtual void NotifyPropertyChanged(string info) 
      { 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs(info)); 
       } 
      } 
     } 

गुण वर्ग भी

2

ऐसा करने का सबसे अच्छा तरीका, IValidatableObject के माध्यम से है। http://msdn.microsoft.com/en-us/data/gg193959.aspx

+0

के लिए IValidalidableObject निश्चित रूप से एक अच्छा समाधान है, लेकिन संभावित पुन: प्रयोज्यता के आधार पर एक सत्यापन प्रमाणीकरण बहुत अधिक करेगा – WiiMaxx

संबंधित मुद्दे