Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simon Ince's blog post seems to be outdated.</p> <p>There is no need to use DataAnnotationsModelValidator or do a DataAnnotationsModelValidator registration.</p> <p>You can use the following code:</p> <pre><code>[AttributeUsage(AttributeTargets.Property, AllowMultiple=false)] public class RequiredIfAttribute : ValidationAttribute, IClientValidatable { private const string _defaultErrorMessage = "'{0}' is required when {1} equals {2}."; public string DependentProperty { get; set; } public object TargetValue { get; set; } public RequiredIfAttribute(string dependentProperty, object targetValue):base(_defaultErrorMessage) { this.DependentProperty = dependentProperty; this.TargetValue = targetValue; } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, DependentProperty, TargetValue); } protected override ValidationResult IsValid(object value, ValidationContext context) { if (context.ObjectInstance != null) { Type type = context.ObjectInstance.GetType(); PropertyInfo info = type.GetProperty(DependentProperty); object dependentValue; if (info != null) { dependentValue = info.GetValue(context.ObjectInstance, null); if (object.Equals(dependentValue, TargetValue)) { if (string.IsNullOrWhiteSpace(Convert.ToString(value))) { return new ValidationResult(ErrorMessage); } } } } return ValidationResult.Success; } public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { ModelClientValidationRule rule = new ModelClientValidationRule(); rule.ErrorMessage = this.FormatErrorMessage(metadata.PropertyName); rule.ValidationType = "requiredif"; rule.ValidationParameters.Add("depedentproperty", DependentProperty); rule.ValidationParameters.Add("targetvalue", TargetValue); yield return rule; } } </code></pre> <p>and the javascript side: if you are using jquery:</p> <pre><code> $.validator.unobtrusive.adapters.add('requiredif', ['depedentproperty', 'targetvalue'], function (options) { options.rules["required"] = function (element) { return $('#' + options.params.depedentproperty).val() == options.params.targetvalue }; if (options.message) { options.messages["required"] = options.message; } $('#' + options.params.depedentproperty).blur(function () { $('#' + options.element.name).valid(); }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload