Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of [compare(" ")] data annotation in .net?

What is the opposite/negate of [Compare(" ")] data annotation" in ASP.NET?

i.e: two properties must hold different values.

public string UserName { get; set; }

[Something["UserName"]]
public string Password { get; set; }
like image 841
RollerCosta Avatar asked Jan 09 '12 09:01

RollerCosta


People also ask

What are .NET annotations?

DataAnnotations is used to configure your model classes, which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC, which allows these applications to leverage the same annotations for client-side validations.

What are data annotations in asp net?

In ASP.NET MVC, Data Annotation is used for data validation for developing web-based applications. We can quickly apply validation with the help of data annotation attribute classes over model classes.

What are annotations in C#?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.


2 Answers

This is the implementation (server side) of the link that @Sverker84 referred to.

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class UnlikeAttribute : ValidationAttribute
{
    private const string DefaultErrorMessage = "The value of {0} cannot be the same as the value of the {1}.";

    public string OtherProperty { get; private set; }

    public UnlikeAttribute(string otherProperty)
        : base(DefaultErrorMessage)
    {
        if (string.IsNullOrEmpty(otherProperty))
        {
            throw new ArgumentNullException("otherProperty");
        }

        OtherProperty = otherProperty;
    }

    public override string FormatErrorMessage(string name)
    {
        return string.Format(ErrorMessageString, name, OtherProperty);
    }

    protected override ValidationResult IsValid(object value,
        ValidationContext validationContext)
    {
        if (value != null)
        {
            var otherProperty = validationContext.ObjectInstance.GetType()
                .GetProperty(OtherProperty);

            var otherPropertyValue = otherProperty
                .GetValue(validationContext.ObjectInstance, null);

            if (value.Equals(otherPropertyValue))
            {
                return new ValidationResult(
                    FormatErrorMessage(validationContext.DisplayName));
            }
        }

        return ValidationResult.Success;
    }
}

Usage:

public string UserName { get; set; }

[Unlike("UserName")]
public string AlternateId { get; set; } 

Details about this implementation, and how to implement it client-side can be found here:

http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2

http://www.macaalay.com/2014/02/25/unobtrusive-client-and-server-side-not-equal-to-validation-in-mvc-using-custom-data-annotations/

like image 88
Eitan K Avatar answered Oct 20 '22 05:10

Eitan K


You can use the [NotEqualTo] data annotation operator included in MVC Foolproof Validation. I used it right now and it works great!

MVC Foolproof is an open source library created by @nick-riggs and has a lot of available validators. Besides doing server side validation it also does client side unobtrusive validation.

Full list of built in validators you get out of the box:

Included Operator Validators

[Is]
[EqualTo]
[NotEqualTo]
[GreaterThan]
[LessThan]
[GreaterThanOrEqualTo]
[LessThanOrEqualTo]

Included Required Validators

[RequiredIf]
[RequiredIfNot]
[RequiredIfTrue]
[RequiredIfFalse]
[RequiredIfEmpty]
[RequiredIfNotEmpty]
[RequiredIfRegExMatch]
[RequiredIfNotRegExMatch]

Note: if you plan to use MVC Foolproof lib and support Localization, make sure you apply the patch I provided here: https://foolproof.codeplex.com/SourceControl/list/patches

like image 31
Leniel Maccaferri Avatar answered Oct 20 '22 04:10

Leniel Maccaferri