Bootstrap 5“水平表单”未显示验证错误

6rvt4ljy  于 2023-01-28  发布在  Bootstrap
关注(0)|答案(1)|浏览(178)

我使用Bootstrap 5示例来水平放置标签和输入字段。
我强制错误类测试错误的位置。
无论我尝试了什么,错误都没有显示。该字段有红色边框和错误图标,但没有显示错误消息。
如果字段不是这样水平的,则会显示错误:

<div class="col-md-6">
     <label for="password" class="form-label">Password</label>
     <input type="text" class="form-control is-invalid" id="password" aria-describedby="passwordError">
     <div id="passwordError" class="invalid-feedback">
          <strong>Error here</strong>
     </div>
</div>

那么你如何显示水平样式的错误呢?

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="m-4">
  <form method="POST" action="#" id="loginForm" novalidate>
    <div class="row g-3 align-items-center">
      <div class="col-auto">
        <label for="password" class="col-form-label">Password</label>
      </div>
      <div class="col-auto">
        <input type="password" id="password" class="form-control is-invalid" aria-describedby="passwordError" autocomplete="current-password">
      </div>
      <div class="col-auto">
        <span id="passwordHelp" class="form-text">
        Must be 8-20 characters long.
      </span>
      </div>
      <div id="passwordError" class="invalid-feedback" role="alert">
        <strong>Error here</strong>
      </div>
    </div>
  </form>
</div>
oaxa6hgo

oaxa6hgo1#

使用同级选择器显示验证,即如下所示:.is-invalid ~ .invalid-feedback,因此is-invalid类需要属于作为确认反馈的兄弟的元素。
这应该行得通:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

<div class="m-4">
  <form method="POST" action="#" id="loginForm" novalidate>
    <div class="row g-3 align-items-center">
      <div class="col-auto">
        <label for="password" class="col-form-label">Password</label>
      </div>
      <div class="col-auto is-invalid">
        <input type="password" id="password" class="form-control is-invalid" aria-describedby="passwordError" autocomplete="current-password">
      </div>
      <div class="col-auto">
        <span id="passwordHelp" class="form-text">
        Must be 8-20 characters long.
      </span>
      </div>
      <div id="passwordError" class="invalid-feedback" role="alert">
        <strong>Error here</strong>
      </div>
    </div>
  </form>
</div>

据我所知,还没有人实际记录验证是如何为水平表单工作的。

相关问题