我已经开始使用Bootstrap来实现一个漂亮的页面设计,而不诉诸GWT(后端是用java制作的)

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

我不确定这个想法是在输入元素中用红色显示错误消息,还是在输入元素下面显示它,或者可能用一个弹出框?


当前回答

引导V3:

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

<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>

这是它的样子:

一旦我完成了它,我想我应该实现它在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.
}
}

View

<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>

输出:

其他回答

你也可以使用下面的类,同时使用bootstrap模态类(v 3.3.7)…联机帮助和帮助块在模态中不起作用。

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

输出如下所示:

对于Bootstrap v4使用: 表单组包装有危险, Form-control-danger for input显示图标(将在输入结束时显示控制面板), 表单-控制-反馈到消息包装器

例子:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="匿名"> < span class=" font - family -宋体"> <input type="text" class="form-control form-control-danger"> <div class="form-control-feedback">无效:(</div> < / div >

引导V3:

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

<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>

这是它的样子:

一旦我完成了它,我想我应该实现它在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.
}
}

View

<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>

输出:

可以使用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>

一般来说,在错误发生的地方附近显示错误是最好的。例如,如果有人在输入电子邮件时出错,你可以突出显示电子邮件输入框。

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

此外,twitter bootstrap有一些很好的样式来帮助这一点(向下滚动到验证状态部分) http://twitter.github.com/bootstrap/base-css.html#forms

突出显示每个输入框有点复杂,所以简单的方法是在顶部放置引导提醒,详细说明用户做错了什么。 http://twitter.github.com/bootstrap/components.html#alerts