Welcome Guest, Not a member yet? Register   Sign In
[Solved] Form Validation Callback With Array "selected[]"
#1

(This post was last modified: 04-05-2015, 07:35 PM by wolfgang1983.)

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

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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


 
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#3

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB