我正在尝试通过配置验证表单。当我提交空白输入字段时,它只是同一页面。我需要在add_article.php帮助中显示错误....
配置/ form_validation.php
Species stat1 stat2
1 setosa 5.006000 73.1
2 versicolor 5.936000 213.0
3 virginica 6.588000 277.6
4 All 5.843333 563.7
我的控制器(admin.php)
<?php
$config = [
'add_article_rules' => [
[
'field' => 'title',
'label' => 'Article Title',
'rules' => 'required|alphadash'
],
[
'field' => 'body',
'label' => 'Article Body',
'rules' => 'required'
]
]
];
查看文件(add_article.php)
<?php
class Admin extends MY_Controller{
public function __construct(){
parent::__construct();
if(! $this->session->userdata('user_id')){
return redirect('login');
}
}
public function dashboard(){
$this->load->model('articlesmodel','articles');
$articles= $this->articles->article_list();
$this->load->view('admin/dashboard',['articles'=>$articles]);
}
public function add_article(){
$this->load->view('admin/add_article');
}
public function store_article(){
$this->load->library('form_validation');
if($this->form_validation->run('add_article_rules')){
//if sucesss
}else{
return redirect('admin/add_article');
}
}
public function edit_select(){}
public function edit_article(){}
public function delete_article(){}
}
答案 0 :(得分:0)
// Just Change Admin Controller method and view file
// View File in add_article.php
<?php echo form_open(); ?>
//Controller File
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
}
public function add_article()
{
if ($this->form_validation->run('add_article_rules')) {
echo "Success";
} else {
$this->load->view('admin/add_article');
}
}
public function edit_select()
{
}
public function edit_article()
{
}
public function delete_article()
{
}
}