Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to adapt the form_dropdown to my form in codeigniter. When i'm adding a new page, it doesn't show any errors in parent dropdown section, but after form validation, for instance if user forgets to enter the title label, if its empty or, after validation it gives foreach error in the dropdown section.

I've read that, the second variable must be array and so on for the form_dropdown. But mine is array and not giving any errors in the page before validation. So i can't figure the problem out.

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();
}
}

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">&times;</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 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);
}
}

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 as $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);?>
share|improve this question
    
My guess is your validation code isn't passing the pages_no_parents array back, validation causes a complete refresh of the page so any variables you need for the page to run need to be regenerated in the if validation == false code. It would help to see your validation code too. –  Rick Calder Aug 7 '13 at 13:55
    
You're overwriting something, it isn't clear as you haven't shown the complete function in your controller. As for the view part, set_value automatically take care of the post thing & as I remember it doesn't take a 3rd argument. –  ahmad Aug 7 '13 at 20:52
    
you were right ahmad. thank you. –  user1621727 Aug 9 '13 at 10:34

3 Answers 3

In the if condition $this->form_validation->run == false write:

$this->data['pages_no_parents'] = $this->page_m->get_no_parents();
share|improve this answer
    
it hasn't worked. –  user1621727 Aug 7 '13 at 14:04
    
Put this $this->data['pages_no_parents'] = $this->page_m->get_no_parents(); in page function of admin controller –  Nil'z Aug 7 '13 at 14:13
    
still the same, can't understand.. –  user1621727 Aug 7 '13 at 14:59
    
Update your answer with your controller please. Need that too. @CastielMartin –  Nil'z Aug 8 '13 at 6:37
    
i've updated my controller, thanks –  user1621727 Aug 8 '13 at 23:13

you can try something like this

if($this->form_validation->run() == FALSE)
{ 
    redirect('your_controller/function','refresh');
}

UPDATE

so instead of redirect do load view like

$this->load->view('admin/_layout_main', $this->data);

and in view page do one thing

<?php echo validation_errors(); ?>

please let me know if you face any problem.

share|improve this answer
    
thats making an infinite loop. –  user1621727 Aug 8 '13 at 23:15
    
please see my update and let me know if its working for you. –  ABorty Aug 9 '13 at 6:26
up vote 0 down vote accepted

As ahmad mentioned, i think i was overwriting something, and after removing set value, and changed view to this, it's now okay.

<?php echo form_dropdown('parent_id', $pages_no_parents, $this->input->post('parent_id') ? $this->input->post('parent_id') : $page->parent_id);  ?>

Thanks for all for the comments and answers.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.