2012-01-23 12 views
2

我期待在MVC 3中獲得一些自定義驗證屬性,以便僅在提交時觸發,而不是在輸入丟失焦點時觸發。在MVC3中獲取自定義驗證屬性fire僅在提交而不是在輸入失去焦點時

這是我的自定義驗證屬性。

#region Password Match Validation Attribute 
    public sealed class PasswordPresent : ValidationAttribute, IClientValidatable 
    { 
     private const string _defaultErrorMessage = "Current Password no present"; 
     private string _basePropertyName; 
     public PasswordPresent(string basePropertyName) 
      : base(_defaultErrorMessage) 
     { 
      _basePropertyName = basePropertyName; 
     } 
     public override string FormatErrorMessage(string name) 
     { 
      return string.Format(_defaultErrorMessage, name, _basePropertyName); 
     } 
     protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
     { 
      var basePropertyInfo = validationContext.ObjectType.GetProperty(_basePropertyName); 
      var oldPassword = (string)basePropertyInfo.GetValue(validationContext.ObjectInstance, null); 
      var newPassword = (string)value; 

      if (!string.IsNullOrEmpty(newPassword) && string.IsNullOrEmpty(oldPassword)) 
      { 
       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()); 

      //This string identifies which Javascript function to be executed to validate this 
      rule.ValidationType = "passwordpresent"; 
      rule.ValidationParameters.Add("otherfield", _basePropertyName); 
      yield return rule; 
     } 
    } 
    #endregion 

下面是客戶端驗證

jQuery.validator.addMethod("passwordmatch", function (value, element, param) { 

    if (value == null || value.length == 0) { 
     return true; 
    } 
    return value == $(param).val(); ; 

}); 


jQuery.validator.unobtrusive.adapters.add("passwordmatch", ["otherfield"], function (options) { 
    options.rules["passwordmatch"] = "#" + options.params.otherfield; 
    options.messages["passwordmatch"] = options.message; 
}); 

的.js文件這裏是我的模型

public class UserSettingsModel 
    { 

     [Display(Name = "Old Password")] 
     [PasswordPresent("NewPassword",ErrorMessage = "Please enter in a New Password")] 
     public string CurrentPassword { get; set; } 

     [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
     [DataType(DataType.Password)] 
     [Display(Name = "New password")] 
     public string NewPassword { get; set; } 

     [DataType(DataType.Password)] 
     [Display(Name = "Confirm new password")] 
     [PasswordsMatch("NewPassword",ErrorMessage = "Confirm password and New Password do not match")] 
     public string ConfirmPassword { get; set; } 

    } 

這裏是我的看法。

@model Data.Models.UserSettingsModel 
    <div id="UserSettings"> 
@using (Ajax.BeginForm("UpdateSettings", "AccountManagement", new AjaxOptions() {UpdateTargetId = "UserSettings", HttpMethod = "Post" })) 
{ 
     @Html.ValidationSummary(true) 

     <div class="tabs" id="userTab3"> 
      <fieldset> 
       <div> 
        <h3> 
         Change Password</h3> 
        <div class="editor-label"> 
         @Html.LabelFor(model => model.CurrentPassword) 
        </div> 
        <div class="editor-field"> 
         @Html.EditorFor(model => model.CurrentPassword) 
         @Html.ValidationMessageFor(model => model.CurrentPassword) 
        </div> 

        <div class="editor-label"> 
         @Html.LabelFor(model => model.NewPassword) 
        </div> 
        <div class="editor-field"> 
         @Html.EditorFor(model => model.NewPassword) 
         @Html.ValidationMessageFor(model => model.NewPassword) 
        </div> 
        <div class="editor-label"> 
         @Html.LabelFor(model => model.ConfirmPassword) 
        </div> 
        <div class="editor-field"> 
         @Html.EditorFor(model => model.ConfirmPassword) 
         @Html.ValidationMessageFor(model => model.ConfirmPassword) 
        </div> 
       </div> 

       <p> 
        <input type="submit" value="Save & Continue" name="btnsubmit" /> 
       </p> 
      </fieldset> 
     </div> 
} 
</div> 

回答

相關問題