[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)] public sealed class RequiredIfAttribute : ValidationAttribute, IClientValidatable { //, . private const string _defaultErrorMessage = "{0} is required"; // private string _targetPropertyName; // private bool _targetPropertyCondition; public RequiredIfAttribute(string targetPropertyName, bool targetPropertyCondition) : base(_defaultErrorMessage) { this._targetPropertyName = targetPropertyName; this._targetPropertyCondition = targetPropertyCondition; } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, name, _targetPropertyName, _targetPropertyCondition); } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { bool result = false; bool propertyRequired = false; // . // var targetProperty = validationContext.ObjectType.GetProperty(_targetPropertyName); // var targetPropertyValue = (bool)targetProperty.GetValue(validationContext.ObjectInstance, null); // , . if (targetPropertyValue == _targetPropertyCondition) { propertyRequired = true; } if (propertyRequired) { // if (value == null) { var message = FormatErrorMessage(validationContext.DisplayName); return new ValidationResult(message); } } return null; } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule(); rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()); rule.ValidationType = "requiredif"; rule.ValidationParameters.Add("targetpropertyname", this._targetPropertyName); rule.ValidationParameters.Add("targetpropertyvalue", this._targetPropertyCondition.ToString().ToLower()); yield return rule; } }
$(function () { $.validator.addMethod("requiredif", function (value, element, param) { if ($(param.propertyname).is(':checked').toString() == param.propertyvalue) { if (!this.depend(param, element)) return "dependency-mismatch"; switch (element.nodeName.toLowerCase()) { case 'select': var val = $(element).val(); return val && val.length > 0; case 'input': if (this.checkable(element)) return this.getLength(value, element) > 0; default: return $.trim(value).length > 0; } } return true; }); $.validator.unobtrusive.adapters.add("requiredif", ["targetpropertyname", "targetpropertyvalue"], function (options) { if (options.element.tagName.toUpperCase() !== "INPUT" || options.element.type.toUpperCase() !== "CHECKBOX") { options.rules["requiredif"] = { propertyname: "#" + options.params.targetpropertyname, propertyvalue: options.params.targetpropertyvalue }; options.messages["requiredif"] = options.message; } }); } (jQuery));
public class RegisterModel { [Required] [Display(Name = "First name")] public string FirstName { get; set; } [Required] [Display(Name = "Last name")] public string LastName { get; set; } [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] [RegularExpression("^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$", ErrorMessage = "Not a valid email.")] public string Email { get; set; } [Display(Name = "Dealer")] public bool IsDealer { get; set; } [RequiredIf("IsDealer", true)] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)] [Display(Name = "Dealer name")] public string Name { get; set; } }
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.requiredif.js")" type="text/javascript"></script>
<appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings>
HtmlHelper.ClientValidationEnabled = true; HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
Source: https://habr.com/ru/post/129398/
All Articles