When I submit my form I would like to use codeigniter form validation set message with callback.
I am having issue when my check box is in array i.e name="selected[]" will not submit callback check.
It works callback validation works fine with single post but not with multiple post array.
Still using codeigniter form validation set message what would be best way to achieve it so message will show.
<?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) {
redirect('admin/user_group');
} 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;
}
}
}
View
<?php echo Modules::run('admin/common/header/index');?><?php echo Modules::run('admin/common/column_left/index');?>
<div id="content">
<?php $data = array('class' => "form-horizontal");?>
<?php echo form_open_multipart('admin/user_group/delete', $data);?>
<div class="page-header">
<div class="container-fluid">
<div class="pull-right">
<a href="<?php echo base_url('admin/user_group/add');?>" role="button" class="btn btn-success"><i class="fa fa-plus"></i></a>
<button type="submit" class="btn btn-danger"><i class="fa fa-trash"></i></button>
</div>
<h1><?php echo $title; ?></h1>
<ul class="breadcrumb">
<?php foreach ($breadcrumbs as $breadcrumb) { ?>
<li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
<?php } ?>
</ul>
</div>
</div>
<div class="container-fluid">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<div class="pull-left">
<h1 class="panel-title"><?php echo $title;?></h1>
</div>
<div class="pull-right">
</div>
</div>
<div class="panel-body">
<?php echo Modules::run('admin/message/messages/index');?>
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
<th style="color: #1e91cf;">User Group Name</th>
<td class="text-right">Action</td>
</tr>
</thead>
<tbody>
<?php if ($user_groups) { ?>
<?php foreach ($user_groups as $user_group) { ?>
<tr>
<td class="text-center"><?php if (in_array($user_group['user_group_id'], $selected)) { ?>
<input type="checkbox" name="selected[]" value="<?php echo $user_group['user_group_id']; ?>" checked="checked" />
<?php } else { ?>
<input type="checkbox" name="selected[]" value="<?php echo $user_group['user_group_id']; ?>" />
<?php } ?></td>
<td class="text-left"><?php echo $user_group['name']; ?></td>
<td class="text-right"><a href="<?php echo $user_group['edit']; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a></td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td class="text-center" colspan="3">No Results</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php echo form_close();?>
</div>
<?php echo Modules::run('admin/common/footer/index');?>
I have solved my problem I had to reload the main view into to the delete form validation false area i.e $this->index();
For some reason when submitted the form the when error the redirect was clearing the error. Al fixed now
<?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;
}
}
}