使用Bootstrap在表单中标记错误

时间:2013-01-16 15:10:29

标签: twitter-bootstrap

我已经开始使用Bootstrap来实现一个不错的页面设计而不需要使用GWT(后端是用java制作的)

对于我的登录屏幕,我复制了此example。现在我想标记一个错误,例如,用户名是空的。所以我想知道Bootstrap框架中的程序是什么。或者,如果有示例显示表单有错误。

我不确定这个想法是用红色在输入元素中显示错误信息,还是在输入元素下方显示错误信息,或者是否有弹出窗口?

7 个答案:

答案 0 :(得分:216)

(使用Bootstrap v4,v3和v3的示例更新)

具有过去几个主要版本Bootstrap的验证类的表单示例。

Bootstrap v4

请参阅codepen上的live version

bootstrap v4 form validation

<div class="container">
  <form>
    <div class="form-group row">
      <label for="inputEmail" class="col-sm-2 col-form-label text-success">Email</label>
      <div class="col-sm-7">
        <input type="email" class="form-control is-valid" id="inputEmail" placeholder="Email">
      </div>
    </div>

    <div class="form-group row">
      <label for="inputPassword" class="col-sm-2 col-form-label text-danger">Password</label>
      <div class="col-sm-7">
        <input type="password" class="form-control is-invalid" id="inputPassword" placeholder="Password">
      </div>
      <div class="col-sm-3">
        <small id="passwordHelp" class="text-danger">
          Must be 8-20 characters long.
        </small>      
      </div>
    </div>
  </form>
</div>

Bootstrap v3

请参阅codepen上的live version

bootstrap v3 form validation

<form role="form">
  <div class="form-group has-warning">
    <label class="control-label" for="inputWarning">Input with warning</label>
    <input type="text" class="form-control" id="inputWarning">
    <span class="help-block">Something may have gone wrong</span>
  </div>
  <div class="form-group has-error">
    <label class="control-label" for="inputError">Input with error</label>
    <input type="text" class="form-control" id="inputError">
    <span class="help-block">Please correct the error</span>
  </div>
  <div class="form-group has-info">
    <label class="control-label" for="inputError">Input with info</label>
    <input type="text" class="form-control" id="inputError">
    <span class="help-block">Username is taken</span>
  </div>
  <div class="form-group has-success">
    <label class="control-label" for="inputSuccess">Input with success</label>
    <input type="text" class="form-control" id="inputSuccess" />
    <span class="help-block">Woohoo!</span>
  </div>
</form>

Bootstrap v2

请参阅jsfiddle上的live version

bootstrap v2 form validation

.error.success.warning.info类会附加到.control-group。这是v2中的标准Bootstrap标记和样式。只要遵循这一点,你就会很好。当然,如果您愿意,可以使用自己的样式来添加弹出窗口或“内联闪存”,但如果您遵循Bootstrap约定并将这些验证类挂在.control-group上,它将保持一致且易于管理(至少,因为你将继续享受Bootstrap文档和示例的好处)

  <form class="form-horizontal">
    <div class="control-group warning">
      <label class="control-label" for="inputWarning">Input with warning</label>
      <div class="controls">
        <input type="text" id="inputWarning">
        <span class="help-inline">Something may have gone wrong</span>
      </div>
    </div>
    <div class="control-group error">
      <label class="control-label" for="inputError">Input with error</label>
      <div class="controls">
        <input type="text" id="inputError">
        <span class="help-inline">Please correct the error</span>
      </div>
    </div>
    <div class="control-group info">
      <label class="control-label" for="inputInfo">Input with info</label>
      <div class="controls">
        <input type="text" id="inputInfo">
        <span class="help-inline">Username is taken</span>
      </div>
    </div>
    <div class="control-group success">
      <label class="control-label" for="inputSuccess">Input with success</label>
      <div class="controls">
        <input type="text" id="inputSuccess">
        <span class="help-inline">Woohoo!</span>
      </div>
    </div>
  </form>

答案 1 :(得分:145)

Bootstrap V3

Official Doc Link 1
Official Doc Link 2

<div class="form-group has-success">
  <label class="control-label" for="inputSuccess">Input with success</label>
  <input type="text" class="form-control" id="inputSuccess" />
  <span class="help-block">Woohoo!</span>
</div>
<div class="form-group has-warning">
  <label class="control-label" for="inputWarning">Input with warning</label>
  <input type="text" class="form-control" id="inputWarning">
  <span class="help-block">Something may have gone wrong</span>
</div>
<div class="form-group has-error">
  <label class="control-label" for="inputError">Input with error</label>
  <input type="text" class="form-control" id="inputError">
  <span class="help-block">Please correct the error</span>
</div>

答案 2 :(得分:13)

使用bootstrap模态类(v 3.3.7)时,也可以使用以下类... help-inline和help-block在模态中不起作用。

<span class="error text-danger">Some Errors related to something</span>

输出如下所示:

Example of error text in modal

答案 3 :(得分:4)

Bootstrap V3:

一旦我在搜索laravel功能,我就会知道这个惊人的表单验证。后来,我修改了glyphicon图标功能。现在,它看起来很棒。

<div class="col-md-12">
<div class="form-group has-error has-feedback">
    <input id="enter email" name="email" type="text" placeholder="Enter email" class="form-control ">
    <span class="glyphicon glyphicon-remove form-control-feedback"></span>       
    <span class="help-block"><p>The Email field is required.</p></span>                                        
</div>
</div>
<div class="clearfix"></div>

<div class="col-md-6">
<div class="form-group has-error has-feedback">
    <input id="account_holder_name" name="name" type="text" placeholder="Name" class="form-control ">
    <span class="glyphicon glyphicon-remove form-control-feedback"></span>                                            
    <span class="help-block"><p>The Name field is required.</p></span>                                        
</div>
</div>
<div class="col-md-6">
<div class="form-group has-error has-feedback">
 <input id="check_np" name="check_no" type="text" placeholder="Check no" class="form-control ">
 <span class="glyphicon glyphicon-remove form-control-feedback"></span>
<span class="help-block"><p>The Check No. field is required.</p></span>                                       
</div>
</div>

这就是它的样子: enter image description here

一旦我完成它,我想我也应该在Codeigniter中实现它。所以这是使用Bootstrap的Codeigniter-3验证:

<强>控制器

function addData()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|max_length[128]');
if($this->form_validation->run() == FALSE)
{
//validation fails. Load your view.
$this->loadViews('Load your view','pass your data to view if any');
}
else
{  
 //validation pass. Your code here.
}
}

查看

<div class="col-md-12">
<?php 
 $email_error = (form_error('email') ? 'has-error has-feedback' : '');
 if(!empty($email_error)){
 $emailData = '<span class="help-block">'.form_error('email').'</span>';
 $emailClass = $email_error;
 $emailIcon = '<span class="glyphicon glyphicon-remove form-control-feedback"></span>';
}
else{
    $emailClass = $emailIcon = $emailData = '';
 } 
 ?>
<div class="form-group <?= $emailClass ?>">
<input id="enter email" name="email" type="text" placeholder="Enter email" class="form-control ">
<?= $emailIcon ?>
<?= $emailData ?>
</div>
</div>

<强>输出: enter image description here

答案 4 :(得分:3)

通常在错误发生的位置附近显示错误是最好的。即如果有人在输入电子邮件时出错,请突出显示电子邮件输入框。

这篇文章有几个很好的例子。 http://uxdesign.smashingmagazine.com/2011/05/27/getting-started-with-defensive-web-design/

另外,twitter bootstrap有一些不错的样式可以帮助解决这个问题(向下滚动到Validation states部分) http://twitter.github.com/bootstrap/base-css.html#forms

突出显示每个输入框有点复杂,因此最简单的方法是在顶部放置一个引导警报,并提供用户错误的详细信息。 http://twitter.github.com/bootstrap/components.html#alerts

答案 5 :(得分:1)

对于Bootstrap v4使用:
has-danger包装的form-group
form-control-danger输入显示图标(在输入结束时显示✖),
邮件包装器form-control-feedback

示例:

&#13;
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">


<div class="form-group has-danger">
  <input type="text" class="form-control form-control-danger">
  <div class="form-control-feedback">Not valid :(</div>
</div>
&#13;
&#13;
&#13;

答案 6 :(得分:0)

可以使用CSS仅在出错时显示错误消息。

.form-group.has-error .help-block {
    display: block;
}

.form-group .help-block {
    display: none;
}


<div class="form-group has-error">
  <label class="control-label" for="inputError">Input with error</label>
  <input type="text" class="form-control" id="inputError">
  <span class="help-block">Please correct the error</span>
</div>