There are two parts. First, you must write the required attribute, which is only required if another property meets your criteria.
You will need to do something like:
public class RequiredComparerAttribute : RequiredAttribute { public OtherProperty { get; set; } public override bool IsValid(object value) {
Then in Application_Start in Global.asax you will need to register a validator, which you can simply reuse the RequiredAttribute validator:
DataAnnotationsModelValidatorProvider .RegisterAdapter(typeof(RequiredComparerAttribute), typeof(RequiredAttributeAdapter));
If you want to add your own validator, you will have to write your own validator. Phil Haack has an example on his blog: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx
Edit: Take a look at CompareAttribute in the .NET Reflector to see how to get the value of OtherProperty. CompareAttribute also implements IClientValidatable to provide the validation rules that are needed on the client side.
I do not think CompareAttribute will work for you because you need to check that the value is required based on the contents of another property, and not compare the equality of the two properties.
Edit2: What does the validation provider do?
He adds rules to the form and provides messages for these rules. You can see exactly how the RequiredAttributeAdapter does this by loading the MVC 3 source. To understand what it does on the client side, you can open the MVC 3 page in Google Chrome, press CTRL + SHIFT + J to open the developer tool window and enter:
$('form:first').data().unobtrusiveValidation.options
The rule object inside the parameters indicates how to check each element, and the message object indicates the error message that will be displayed for each verification error.
Edit3: Full Example
After answering this question, I wrote a blog post with a complete example of creating a user attribute on the client (unobtrusive check) and the server. Blog post here . This example is for the 'contains' attribute, but it's pretty easy to change to become a necessary mapping.