php - Codeigniter Form Dropdown Error After Validation -
i'm trying adapt form_dropdown form in codeigniter. when i'm adding new page, doesn't show errors in parent dropdown section, after form validation, instance if user forgets enter title label, if empty or, after validation gives foreach error in dropdown section.
i've read that, second variable must array , on form_dropdown. mine array , not giving errors in page before validation. can't figure problem out.
a php error encountered severity: warning message: invalid argument supplied foreach() filename: helpers/form_helper.php line number: 331
my_controller :
class my_controller extends ci_controller{ public $data = array(); function __construct(){ parent::__construct(); } }
admin controller :
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'); } } } }
controller :
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 page or set new 1 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 dropdown $this->data['pages_no_parents'] = $this->page_m->get_no_parents(); // set form $rules = $this->page_m->rules; $this->form_validation->set_rules($rules); // process 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 view $this->data['subview'] = 'admin/page/edit'; $this->load->view('admin/_layout_main', $this->data); } }
model:
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 $page) { $array[$page->id] = $page->title; } } return $array; }
view :
<?=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);?>
in if condition $this->form_validation->run == false
write:
$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
Comments
Post a Comment