自定义密码ValidationMessageFor

时间:2015-07-31 09:30:36

标签: html asp.net-mvc razor

您好我正在尝试为身份验证系统创建自定义密码验证,具体取决于使用MVC设置的密码设置。

在系统的管理区域中,用户可以选择密码必须遵守的设置。我希望验证消息能够反映出来并且仅在密码输入错误时显示,但目前它始终显示。

这是我的观点:

@model Domain.Authentication.Users.RegisterDTO

@helper passwordPolicySpecs()
{
    Services.Settings.Interfaces.ISettingsService _service = Core.Context.Engine.Resolve<Services.Settings.Interfaces.ISettingsService>();
    string passwordWarning = "Password invalid. Password must contain: ";
    passwordWarning = (_service.GetSettingValue<Services.Authentication.Policies.PasswordPolicySettings, bool>(x => x.PasswordMustBeMinimumLength)) ? passwordWarning + ("At least " + _service.GetSettingValue<Services.Authentication.Policies.PasswordPolicySettings, int>(x => x.PasswordLength).ToString() + " letters. ") : passwordWarning + "";
    passwordWarning = (_service.GetSettingValue<Services.Authentication.Policies.PasswordPolicySettings, bool>(x => x.PasswordMustContainANumber)) ? passwordWarning + "A number. " : passwordWarning + "";
    passwordWarning = (_service.GetSettingValue<Services.Authentication.Policies.PasswordPolicySettings, bool>(x => x.PasswordMustContainASpecialCharacter)) ? passwordWarning + "A special character. " : passwordWarning + "";
    passwordWarning = (_service.GetSettingValue<Services.Authentication.Policies.PasswordPolicySettings, bool>(x => x.PasswordMustContainLowerCase)) ? passwordWarning + "A lower case letter. " : passwordWarning + "";
    passwordWarning = (_service.GetSettingValue<Services.Authentication.Policies.PasswordPolicySettings, bool>(x => x.PasswordMustContainUpperCase)) ? passwordWarning + "An upper case letter. " : passwordWarning + "";
    @Html.ValidationMessageFor(model => model.Password, passwordWarning, new { @class = "text-danger" })
}

<div class="reset-password-page">
    <div class="row">
        <div class="col-xs-12 ">
            <h2>@Core.Helpers.ResourceHelpers.Get("Register")</h2>

            @using (Html.BeginForm("Register", "Authentication", FormMethod.Post, new { id = "RegistrationForm" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)

                <fieldset>
                    <dl class="dl-horizontal">
                        <dd>
                            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", placeholder = @Core.Helpers.ResourceHelpers.Get("EmailAddress") } })
                        </dd>

                        <dd>
                            @passwordPolicySpecs()
                            @Html.PasswordFor(model => model.Password, new { @class = "form-control", placeholder = @Core.Helpers.ResourceHelpers.Get("Password") })
                        </dd>

                        <dd class="padding-top-10">
                            @Html.ValidationMessageFor(model => model.PasswordConfirm, "", new { @class = "text-danger" })
                            @Html.PasswordFor(model => model.PasswordConfirm, new { @class = "form-control", placeholder = @Core.Helpers.ResourceHelpers.Get("ConfirmPassword") })
                        </dd>

                    </dl>

                    <p class="register-btn">
                        @Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" }) <!--Fill in all fields error-->
                        <input type="submit" value="@Core.Helpers.ResourceHelpers.Get("Register")" class="btn btn-default" />
                    </p>
                </fieldset>
            }
        </div>
    </div>
</div>

0 个答案:

没有答案