CodeIgniter Forums

Full Version: [Solved] Form Validation Callback With Array "selected[]"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a post array called selected on my form name="selected[]";  The set message does not work.


I am still unsure on how to use callbacks with array. I trying to understand it.

My callback message works fine with single post but not array[]


How can I make call back pick up name="selected[]"; and display message.


PHP Code:
public function delete() {
$this->form_validation->set_rules('selected[]''''callback_validateDelete');

if (
$this->form_validation->run($this) == FALSE) {

redirect('admin/user_group');

} else {

foreach (
$_POST['selected'] as $user_group_id) {
$this->delete_user_group($user_group_id);
}

$this->session->set_flashdata('success''Success: You have modified user groups!');
redirect('admin/user_group');
}
}

public function 
validateDelete() {

$this->load->library('user');

if (
$this->user->hasPermission('modify'"User_group_list")) {
return 
true;
} else {
$this->form_validation->set_message('validateDelete''Warning: You do not have permission to modify user groups');
return 
false;
}

(04-05-2015, 07:29 AM)riwakawd Wrote: [ -> ]I have a post array called selected on my form name="selected[]";  The set message does not work.


I am still unsure on how to use callbacks with array. I trying to understand it.

My callback message works fine with single post but not array[]


How can I make call back pick up name="selected[]"; and display message.




PHP Code:
public function delete() {
$this->form_validation->set_rules('selected[]''''callback_validateDelete');

if (
$this->form_validation->run($this) == FALSE) {

redirect('admin/user_group');

} else {

foreach (
$_POST['selected'] as $user_group_id) {
$this->delete_user_group($user_group_id);
}

$this->session->set_flashdata('success''Success: You have modified user groups!');
redirect('admin/user_group');
}
}

public function 
validateDelete() {

$this->load->library('user');

if (
$this->user->hasPermission('modify'"User_group_list")) {
return 
true;
} else {
$this->form_validation->set_message('validateDelete''Warning: You do not have permission to modify user groups');
return 
false;
}


I have solved my issue found out why for some reason when I submitted my form if I had error via callback the redirect was clearing it, so therefor no errors were showing up I had to call the main function with the view in it i.e $this->index();


PHP Code:
<?php

class User_group_list extends Admin_Controller {

    public function 
index() {
        
$data['title'] = ucwords(str_replace('_'' '$this->router->fetch_class()));

        
$data['breadcrumbs'] = array();

        
$data['breadcrumbs'][] = array(
            
'text' => 'Home',
            
'href' => site_url('admin/dashboard')
        );

        
$data['breadcrumbs'][] = array(
            
'text' =>  ucwords(str_replace('_'' '$this->router->fetch_class())),
            
'href' => base_url('admin/user_group')
        );

        
$data['user_groups'] = array();

        
$results $this->get_user_groups();

        foreach (
$results as $result) {
            
$data['user_groups'][] = array(
                
'user_group_id' => $result['user_group_id'],
                
'name' => $result['name'],
                
'edit' => site_url('admin/user_group/update' .'/'.$result['user_group_id'])
            );
        }

        if (isset(
$_POST['selected'])) {
            
$data['selected'] = (array)$_POST['selected'];
        } else {
            
$data['selected'] = array();
        }

        
$this->load->view('template/user_group/user_group_list'$data);
    }

    public function 
get_user_groups() {
        
$user_group $this->db->get($this->db->dbprefix 'user_group');
        if (
$user_group->num_rows()) {
            return 
$user_group->result_array();
        } else {
            return 
false;
        }
    }

    public function 
delete() {
        
$this->load->library('form_validation');

        
$selected_post $this->input->post('selected');

        
$this->form_validation->set_rules('selected[]''selected''required|callback_validateDelete');

        if (
$this->form_validation->run($this) == FALSE) {
            
$this->index();
        } else {
            
$this->session->set_flashdata('success''Success: You have modified user groups!');
            
redirect('admin/user_group');
        }
    }

    public function 
delete_user_group($user_group_id) {
        
$this->db->where('user_group_id'$user_group_id);
        
$this->db->delete($this->db->dbprefix 'user_group');
    }

    public function 
validateDelete() {
        
$this->load->library('user');
        
$this->load->library('form_validation');

        if (
$this->user->hasPermission('modify'"User_group_list")) {
            return 
true;
        } else {
            
$this->form_validation->set_message('validateDelete''Warning: You do not have permission to modify user groups');
            return 
false;
        }
        
    }


 
That's standard web behavior. POST and other globals get wiped out on the next page request, which a redirect does. You can also put the validation errors in session flashdata, or a cookie, or something else before redirecting and checking for it in your view, or do what you did to solve it.