Filter error in different password fields in the same view

Asked

Viewed 44 times

0

I have a VIEW called Change Password and in it I have a Current Password field, a New password field and a New Password Confirmation field.

The three fields pass through the same control filter for these password, whose rule is that the password must be a mandatory field and contain between 8 and 15 elements.

My problem is: when the filter is activated in the first password field (if the password is not within the rules), the error message appears in all 3 password fields, and not only in the one being filled in.

Any hint on how to isolate these components and make the error message only be shown in the field that does not have the correct information?

<div class="row">
    <div class="col-sm-12 col-sm-offset-4"><h2>Alterar senha do sistema</h2></div>
</div>
<br>

<div class="container">
    <div class="middle">

<form>

            <!--campo senha atual-->
            <div class="row">
                <div class="col-sm-3 col-sm-offset-4">
                    <div class="form-group">
                        <label>Senha Atual</label>
                        <input asp-for="Password" type="password" id="inputPassword" class="form-control" required>
                        @Html.ValidationMessageFor(model => model.Password)
                    </div>
                </div>
            </div>

            <!-- nova senha-->
            <div class="row">
                <div class="col-sm-3 col-sm-offset-4">
                    <div class="form-group">
                        <div class="input-group">
                            <label>Nova Senha</label>
                            <input asp-for="Password" type="password" id="inputPassword" class="form-control" placeholder="Entre 8 e 15 caracteres" required>
                        </div>
                          @Html.ValidationMessageFor(model => model.Password)
                     </div>
                </div>
            </div>

            <!--confirmar nova senha-->
            <div class="row">
                <div class="col-sm-3 col-sm-offset-4">
                    <div class="form-group">
                        <div class="input-group">
                            <label>Confirme a nova senha</label>
                            <input asp-for="Password" type="password" id="inputPassword" class="form-control" placeholder="Entre 8 e 15 caracteres" required>
                        </div>
                            @Html.ValidationMessageFor(model => model.Password)
                    </div>
                </div>
            </div>


            <br>

            <!--botao salvar-->
            <div class="col-sm-1 col-sm-offset-4">
                <button type="submit" class="btn btn-success btn-block">Salvar</button>
            </div>






        </form>

        <!--link cancelar-->
        <div class="col-sm-1">
            <a href="#" class="forgot-password">Cancelar</a>
        </div>


    </div>
</div>

This is my filter for the password field

    #region Senha
        [Required(ErrorMessage = "Campo obrigatório")]
        [StringLength(15, MinimumLength = 8, ErrorMessage = "Sua senha deve ter entre 8 e 15 caracteres.")]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }
    #endregion
  • 1

    mvc = architectural standard / asp.net-mvc = technology specifies that it happens to be based on "mvc", then mvc != asp.net-mvc .......................... Please read the correct usage description when selecting the tags. PS: Do not use "Code snippet" (Stack Snippets) without need, read: http://pt.meta.stackoverflow.com/q/2115/3635. Welcome.

1 answer

0


The error is that all three password fields were calling the same Validation. In this case, I need to create three different templates, one for Current Password, one for New Password and one for Confirmation of New Password, and then call the respective templates for verification.

MODEL

    #region Senha Atual
    [Required(ErrorMessage = "Campo obrigatório")]
    [StringLength(15, MinimumLength = 8, ErrorMessage = "Sua senha deve ter entre 8 e 15 caracteres.")]
    [DataType(DataType.Password)]
    [Display(Name = "SenhaAtual")]
    public string Password { get; set; }
    #endregion

    #region Nova Senha
    [Required(ErrorMessage = "Campo obrigatório")]
    [StringLength(15, MinimumLength = 8, ErrorMessage = "Sua senha deve ter entre 8 e 15 caracteres.")]
    [DataType(DataType.Password)]
    [Display(Name = "NovaSenha")]
    public string NewPassword{ get; set; }
    #endregion

    #region Confirmar Nova Senha
    [Required(ErrorMessage = "Campo obrigatório")]
    [StringLength(15, MinimumLength = 8, ErrorMessage = "Sua senha deve ter entre 8 e 15 caracteres.")]
    [DataType(DataType.Password)]
    [Display(Name = "ConfirmarNovaSenha")]
    public string ConfirmPassword { get; set; }
    #endregion

VIEW

        <!--campo senha atual-->
        <div class="row">
            <div class="col-sm-3 col-sm-offset-4">
                <div class="form-group">
                    <label>Senha Atual</label>
                    @Html.TextBoxFor(model => model.Password, new { @class = "form-control", @type = "password" })
                    @Html.ValidationMessageFor(model => model.Password)
                </div>
            </div>
        </div>

        <!-- nova senha-->
        <div class="row">
            <div class="col-sm-3 col-sm-offset-4">
                <div class="form-group">
                    <label>Nova Senha</label>
                    @Html.TextBoxFor(model => model.NewPassword, new { @class = "form-control", @type = "password" })
                    @Html.ValidationMessageFor(model => model.NewPassword)
                </div>
            </div>
        </div>

        <!--confirmar nova senha-->
        <div class="row">
            <div class="col-sm-3 col-sm-offset-4">
                <div class="form-group">
                    <label>Confirme a nova senha</label>
                    @Html.TextBoxFor(model => model.ConfirmPassword, new { @class = "form-control", @type = "password" })
                    @Html.ValidationMessageFor(model => model.ConfirmPassword)
                </div>
            </div>
        </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.