It turned out that the custom validator method I had did not account for the fact that my input elements had names with underscores in them (due to the fact that my view model class had a property exposing another object containing the properties representing the form fields).
My view model is something like this:
public class MyViewModel
{
public Info Step1 { get; set; }
public class Info
{
public string Property1 { get; set; }
[EitherOrRequired(DependentProperty="Property1")]
public string Property2 { get; set; }
}
}
Which produces input elements with ids of Step1_Property1
and Step1_Property2
. The problem is that the Step1_Property2 input has its data-val-dependentproperty set to "Property1" (the value of the attribute in the view model), so the custom validator method looks for a dependent property which doesn't exist. To overcome this, I modified the custom validator function to ensure that the dependent property has the same prefix as the current property.
$.validator.addMethod("eitherorrequired", function(value, element, params) {
var name = $(element).attr('name').replace('#', '');
var i = name.lastIndexOf('.');
if (i > -1) {
params = '#' + name.substr(0, i).replace('.', '_') + '_' + params.replace('#', '');
}
var otherValue = $(params).val();
return value != "" || otherValue != "";
});