Filter detecting error in different password fields in the same view

0

I have a VIEW call Change Password and I have a Current Password field, a New Password field, and a New Password Confirmation field.

All three fields pass through the same control filter for these passwords, the rule being that the password must be a required 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, not only in the one that is being filled.

How can I isolate these components and cause the error message to only be displayed 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
    
asked by anonymous 26.09.2017 / 21:36

1 answer

0

The error is that all three password fields were calling the same Validation. In case, I need to create three different models, one for Current Password, one for New Password and one for Confirmation of New Password, and then call the respective models 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>
    
28.09.2017 / 20:16