当我尝试验证登录表单时,我创建了一个登录控制器并加载了所有必需的库和帮助程序,它显示了致命的错误未定义validation_errors()
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class LoginController extends CI_Controller{
public function index()
{
$error['error']='';
$this->load->library('form_validation');
$this->load->helper(array('form','url'));
$this->form_validation->set_rules('user','Username','required|trim');
$this->form_validation->set_rules('password','Password','required|trim');
if($this->form_validation->run())
{
echo 'success';
}
else
{
$error['error']='username required';
return redirect('welcome/login','refresh',$error);
}
}
}
这是我的观点
<div class="container">
<?php echo validation_errors()?>
<div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3">
<div class="row">
<div class="iconmelon">
<svg viewBox="0 0 32 32">
<g filter="">
<use xlink:href="#git"></use>
</g>
</svg>
</div>
</div>
<div class="panel panel-default" >
<div class="panel-heading">
<div class="panel-title text-center">Bootsnipp.com<?php echo $error;?> </div>
</div>
答案 0 :(得分:1)
方法1:在此路径中加载自动加载文件中的库application\config\autoload.php
$autoload['libraries'] = array('form_validation');
方法2:在控制器构造函数中加载库。
$this->load->library('form_validation');
1st:您需要加载视图
,而不是重定向return redirect('welcome/login','refresh',$error);
到
$this->load->view('welcome/login');
答案 1 :(得分:1)
签入docs流程应如何显示:
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
在此else块中,您可以将重定向设置为成功页面。如果表单失败,则必须再次加载(在您的情况下为welcome/login
)视图。
所以,你的代码就像:
<?php defined('BASEPATH') or exit('Come over next time');
class Logincontroller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$error['error']='';
$this->load->library('form_validation');
$this->load->helper(array('form','url'));
$this->form_validation->set_rules('user','Username','required|trim');
$this->form_validation->set_rules('password','Password','required|trim');
if($this->form_validation->run() == false) {
// you don't need to initialize error, CI will handle that
$this->load->view('welcome/login');
}
else {
echo 'success';
}
}
答案 2 :(得分:0)
您应该自动加载form_validation
库。
这应该是路径:application \ config \ autoload.php
在$autoload['libraries'] = array();
然后将form_validation添加为此数组中的一个值。
试一试。它应该工作。
答案 3 :(得分:0)
试试这个
类和文件名的第一个字母应该是唯一的大写字母。另外,请确保您已在config.php中设置基本网址,并阅读
上方的评论$config['base_url']
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Logincontroller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->helper(array('form','url'));
}
public function index()
{
$data['error'] = '';
// You can add other data also.
$this->form_validation->set_rules('user','Username','required|trim');
$this->form_validation->set_rules('password','Password','required|trim');
if($this->form_validation->run() == FALSE)
{
$this->load->view('login_view', $data);
}
else
{
// You can redirect to success controller
redirect('welcome');
}
}
}
视图
<?php echo validation_errors();?>
<?php echo form_open('logincontroller');?>
// Form Content
<?php echo form_close();?>