使用 bootstrap 在 codeigniter 中显示 validations_error()
Display the validations_error() in codeigniter with bootstrap
现在有一个表单有 ~10 个字段,验证提示太长,请参考屏幕截图
代码是这样的
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('title_tw', 'Title(繁體)', 'required');
$this->form_validation->set_rules('description_tw', 'Description(繁體)', 'required');
$this->form_validation->set_rules('title_cn', 'Title(简体)', 'required');
$this->form_validation->set_rules('description_cn', 'Description(简体)', 'required');
$this->form_validation->set_rules('pose', 'Pose', 'required');
$this->form_validation->set_rules('style', 'Style', 'required');
$this->form_validation->set_rules('level', 'Level', 'required');
$this->form_validation->set_rules('type', 'Type', 'required');
$this->form_validation->set_rules('teacher', 'Teacher', 'required');
$this->form_validation->set_rules('start_date', 'Start Date', 'required');
if ($this->input->post('type') != false && $this->input->post('type')[0] == 1) {
$this->form_validation->set_rules('price', 'Price (USD)', 'required|numeric|greater_than[0]');
$this->form_validation->set_rules('price_cn', 'Price (RMB)', 'required|numeric|greater_than[0]');
}
$this->form_validation->set_error_delimiters('<div class="alert alert-danger"><a class="close" data-dismiss="alert">x</a><strong>', '</strong></div>');
和前端:
<?= validation_errors; ?>
我想使用 bootstrap 中的手风琴来减小尺寸,那么有什么方法可以覆盖验证的显示吗?
以及如何检查是否有例如超过 3 个错误,比显示例如总共 10 个错误并使用手风琴显示错误消息?
非常感谢。
error_array() 函数 returns 错误消息数组。所以你可以计算那里有多少错误信息,比如;
$errors= error_array();
$numOfErrors = count($errors);
您也可以使用 form_error() 函数分别显示错误到他们的字段。
<?php echo form_error('title'); ?>
<input type="text" name="title">
<?php echo form_error('title_tw'); ?>
<input type="text" name="title_tw">
...
据我了解,您的问题是错误的总高度,因为每个错误都显示在不同的行中。
你可以使用下面的代码在一行中显示错误
<?php if(validation_errors()) { ?>
<div class="alert alert-danger">
<a class="close" data-dismiss="alert">x</a>
<strong><?php echo strip_tags(validation_errors()); ?></strong>
</div>
<?php } ?>
您已经分别显示了每个错误,因此中间占用了太多 space 并且看起来不太好。
所以在这里我将向您展示如何正确显示它,使其看起来和感觉起来都不错,但是 bootstrap 是必需的并且您正在使用它,因此您可以在 codeigniter 中使用下面的代码。
并且您还可以通过按 div 右上角 的关闭按钮关闭它。
<?php if (!empty(validation_errors())): ?>
<div class="alert alert-danger">
<a class="close" data-dismiss="alert" title="close">x</a>
<ul><?php echo (validation_errors('<li>', '</li>')); ?></ul>
</div>
<?php endif; ?>
如果您想看看它的外观,那么我有一个演示输出图像给您:
现在有一个表单有 ~10 个字段,验证提示太长,请参考屏幕截图
代码是这样的
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('title_tw', 'Title(繁體)', 'required');
$this->form_validation->set_rules('description_tw', 'Description(繁體)', 'required');
$this->form_validation->set_rules('title_cn', 'Title(简体)', 'required');
$this->form_validation->set_rules('description_cn', 'Description(简体)', 'required');
$this->form_validation->set_rules('pose', 'Pose', 'required');
$this->form_validation->set_rules('style', 'Style', 'required');
$this->form_validation->set_rules('level', 'Level', 'required');
$this->form_validation->set_rules('type', 'Type', 'required');
$this->form_validation->set_rules('teacher', 'Teacher', 'required');
$this->form_validation->set_rules('start_date', 'Start Date', 'required');
if ($this->input->post('type') != false && $this->input->post('type')[0] == 1) {
$this->form_validation->set_rules('price', 'Price (USD)', 'required|numeric|greater_than[0]');
$this->form_validation->set_rules('price_cn', 'Price (RMB)', 'required|numeric|greater_than[0]');
}
$this->form_validation->set_error_delimiters('<div class="alert alert-danger"><a class="close" data-dismiss="alert">x</a><strong>', '</strong></div>');
和前端:
<?= validation_errors; ?>
我想使用 bootstrap 中的手风琴来减小尺寸,那么有什么方法可以覆盖验证的显示吗?
以及如何检查是否有例如超过 3 个错误,比显示例如总共 10 个错误并使用手风琴显示错误消息?
非常感谢。
error_array() 函数 returns 错误消息数组。所以你可以计算那里有多少错误信息,比如;
$errors= error_array();
$numOfErrors = count($errors);
您也可以使用 form_error() 函数分别显示错误到他们的字段。
<?php echo form_error('title'); ?>
<input type="text" name="title">
<?php echo form_error('title_tw'); ?>
<input type="text" name="title_tw">
...
据我了解,您的问题是错误的总高度,因为每个错误都显示在不同的行中。
你可以使用下面的代码在一行中显示错误
<?php if(validation_errors()) { ?>
<div class="alert alert-danger">
<a class="close" data-dismiss="alert">x</a>
<strong><?php echo strip_tags(validation_errors()); ?></strong>
</div>
<?php } ?>
您已经分别显示了每个错误,因此中间占用了太多 space 并且看起来不太好。
所以在这里我将向您展示如何正确显示它,使其看起来和感觉起来都不错,但是 bootstrap 是必需的并且您正在使用它,因此您可以在 codeigniter 中使用下面的代码。
并且您还可以通过按 div 右上角 的关闭按钮关闭它。
<?php if (!empty(validation_errors())): ?>
<div class="alert alert-danger">
<a class="close" data-dismiss="alert" title="close">x</a>
<ul><?php echo (validation_errors('<li>', '</li>')); ?></ul>
</div>
<?php endif; ?>
如果您想看看它的外观,那么我有一个演示输出图像给您: