简体   繁体   English

根据验证结果切换元素可见性

[英]Toggle element visibility based on validation results

Given the following HTML 鉴于以下HTML

<form class="form-horizontal"
      asp-controller="Installation"
      asp-action="CreateUser"
      method="post">
    <fieldset>

        <!-- Form Name -->
        <legend>Account Creation</legend>

        <!-- Username input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="userName">Username</label>
            <div class="col-md-4">
                <input id="UserName" name="UserName" asp-for="UserName" type="text" placeholder="Username" class="form-control input-md" required="">
                <span id="usernameTip" class="help-block hidden">Enter a unique Username</span>
                <span class="has-error" asp-validation-for="UserName"></span>
            </div>
        </div>

        <!-- Email input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="email">E-mail</label>
            <div class="col-md-4">
                <input id="email"
                       name="email"
                       type="text"
                       placeholder="jane@doe.com"
                       class="form-control input-md"
                       required=""
                       asp-for="Email">
                <span class="help-block">Enter your e-mail address</span>
            </div>
        </div>

        <!-- Password input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="password">Password</label>
            <div class="col-md-4">
                <input id="password"
                       name="password"
                       type="password"
                       placeholder="password"
                       class="form-control input-md"
                       required=""
                       asp-for="Password">
                <span class="help-block">Enter a password that is at least 8 characters, fewer than 30</span>
            </div>
        </div>

        <!-- Password input-->
        <div class="form-group">
            <label class="col-md-4 control-label" for="confirmPassword">Confirm Password</label>
            <div class="col-md-4">
                <input id="confirmPassword"
                       name="confirmPassword"
                       type="password"
                       placeholder="password"
                       class="form-control input-md"
                       required=""
                       asp-for="PasswordConfirmation">
                <span class="help-block">Re-enter your password</span>
            </div>
        </div>

        <!-- Submit -->
        <div class="form-group">
            <label class="col-md-4 control-label"
                   for="createAccount"></label>
            <div class="col-md-4">
                <button id="createAccount" type="submit" name="createAccount" class="btn btn-primary">Create Account</button>
            </div>
        </div>

    </fieldset>
</form>

I would like to hide/show the usernameTip span, when the asp-validation-for span contains a validation error, along with any other span that i'm using as a tip when there is an adjacent asp-validation-for span. 我想隐藏/显示usernameTip跨度,当asp-validation-for跨度包含验证错误时,以及当有相邻的asp-validation-for跨度时,我将其用作提示的任何其他跨度。

Reading this post I can get the JavaScript needed to show/hide the span. 阅读这篇文章,我可以获得显示/隐藏跨度所需的JavaScript。 The only thing I can't figure out is if the View actually has knowledge of the validation errors in a manor that lets me conditionally hide/show that usernameTip span if errors exist. 我唯一不知道的是,View是否真正了解庄园中的验证错误,如果有错误,我可以有条件地隐藏/显示usernameTip范围。

Does anyone know what I need to do in order to toggle the visibility based off the data annotation errors on my model? 有谁知道我需要做些什么才能根据模型上的数据注释错误切换可见性?

When looking in Chrome, asp-validation-for span is turned into this at compile time: 在Chrome浏览器中查看时,编译时将asp-validation-for span转换为:

<span class="has-error field-validation-error" data-valmsg-for="UserName" data-valmsg-replace="true">Usernames must be between 2 and 50 characters</span>

EDIT 编辑

When validation fails, there is a ==$0 appended to the end. 验证失败时,末尾会附加==$0

Usernames must be between 2 and 50 characters == $0 用户名必须介于2到50个字符之间== $ 0

I do not see any classes being added or removed from the span when the validation fails. 验证失败时,我看不到从跨度添加或删除任何类。

Edit 2 编辑2

I got this working using the accepted answer. 我使用可接受的答案进行了这项工作。 For those looking in the future however, you can use MVC's ViewData property in the view to determine if there are errors. 但是,对于未来的用户,可以在视图中使用MVC的ViewData属性来确定是否存在错误。

<div class="col-md-4">
    <input id="UserName" name="UserName" asp-for="UserName" type="text" placeholder="Username" class="form-control input-md" required="">
    @if (ViewData.ModelState[nameof(AccountCreationViewModel.UserName)] == null || ViewData.ModelState[nameof(AccountCreationViewModel.UserName)].Errors.Count == 0)
    {
        <span class="help-block">Enter a unique Username</span>
    }
    else
    {
        <span class="label label-danger" role="alert" asp-validation-for="UserName"></span>
    }
</div>
<div class="col-md-4">
    <input id="UserName" name="UserName" asp-for="UserName" type="text" placeholder="Username" class="form-control input-md" required="">
    <span id="usernameTip" class="help-block hidden">Enter a unique Username</span>
    <span id="error" class="has-error" asp-validation-for="UserName"></span>
</div>
    <script>

   if( $("#error").text().length>0){
//show usernameTip 
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM