我正在尝试将form_dropdown改编为codeigniter中的表单。当我添加新页面时,它不会在父下拉列表中显示任何错误,但在表单验证后,例如,如果用户忘记输入标题标签,如果它为空,或者在验证后它给出了foreach错误下拉部分。
我已经读过,第二个变量必须是数组,依此类推form_dropdown。但我的是数组,并且在验证之前没有在页面中给出任何错误。所以我无法解决问题。
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: helpers/form_helper.php
Line Number: 331
MY_Controller:
class MY_Controller extends CI_Controller{
public $data = array();
function __construct(){
parent::__construct();
}
}
管理员控制器:
class Admin_Controller extends MY_Controller{
function __construct(){
parent::__construct();
$this->data['meta_title'] = 'My Website Control Panel';
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters(' <div class=" alert alert-danger alert-block" ><span class="glyphicon glyphicon glyphicon-minus-sign"></span> ', '<span class="close" data-dismiss="alert">×</span></div>');
$this->load->library('session');
$this->load->model('user_m');
$exceptions = array('admin/user/login', 'admin/user/logout');
if(in_array(uri_string(),$exceptions) == FALSE){
if($this->user_m->loggedin() == FALSE){
redirect('admin/user/login');
}
}
}
}
控制器:
class Page extends Admin_Controller{
public function __construct(){
parent::__construct();
$this->load->model('page_m');
}
public function index(){
$this->data['pages'] = $this->page_m->get_with_parent();
$this->data['subview'] = "admin/page/index";
$this->load->view('admin/_layout_main',$this->data);
}
public function edit ($id = NULL)
{
// Fetch a page or set a new one
if ($id) {
$this->data['page'] = $this->page_m->get($id);
count($this->data['page']) || $this->data['errors'][] = 'Not found.';
}
else {
$this->data['page'] = $this->page_m->get_new();
}
// Pages for dropdown
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
// Set up the form
$rules = $this->page_m->rules;
$this->form_validation->set_rules($rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$data = $this->page_m->array_from_post(array('title', 'slug', 'body','keywords','description','parent_id'));
$this->page_m->save($data, $id);
redirect('admin/page');
}
// Load the view
$this->data['subview'] = 'admin/page/edit';
$this->load->view('admin/_layout_main', $this->data);
}
}
型号:
public function get_no_parents ()
{
// Fetch pages without parents
$this->db->select('id, title');
$this->db->where('parent_id', 0);
$pages = parent::get();
// Return key => value pair array
$array = array(
0 => ''
);
if (count($pages)) {
foreach ($pages as $page) {
$array[$page->id] = $page->title;
}
}
return $array;
}
查看:
<?=form_dropdown('parent_id', $pages_no_parents, set_value('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id);?>
答案 0 :(得分:0)
在if条件中$this->form_validation->run == false
写:
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
答案 1 :(得分:0)
你可以尝试这样的事情
if($this->form_validation->run() == FALSE)
{
redirect('your_controller/function','refresh');
}
<强>更新强>
所以不用重定向来加载视图,如
$this->load->view('admin/_layout_main', $this->data);
并在视图页面做一件事
<?php echo validation_errors(); ?>
如果您遇到任何问题,请告诉我。
答案 2 :(得分:0)
正如艾哈迈德提到的那样,我认为我覆盖了一些东西,并且在删除了设定值之后,并改变了对此的看法,它现在没问题。
<?php echo form_dropdown('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id); ?>
感谢大家的评论和答案。