[eluser]spheroid[/eluser]
Using CI 1.6.3 here is how I got this working with a checkbox array.
Also attaching MY_Validation.php.
Also uses updated form_checkbox:
http://ellislab.com/forums/viewthread/91983/
Controller:
Code:
function dropdown_validate($str)
{
if ($str == 'Select one:')
{
$this->validation->set_message('dropdown_validate', 'Please select a choice from the %s menu.');
return FALSE;
}
else
{
return TRUE;
}
}
function checkbox_validate($str = NULL)
{
if ($str == NULL)
{
$this->validation->set_message('checkbox_validate', 'Please select a choice from the %s options.');
return FALSE;
}
else
{
return TRUE;
}
}
function add()
{
if($this->authAccess("Admin") != TRUE)
{
$data['pageinfo'] = "";
$this->_sendAlert("You don't have permission to add jobs.", $msg_col = "Red");
redirect('job/view/' . $this->uri->segment(3), 'location');
}
else
{
/** Set up rules and field names for validation **/
$rules = array(
'job_title' => "trim|required|xss_clean",
'category' => "trim|callback_dropdown_validate|xss_clean",
'skill' => "trim|callback_checkbox_validate|xss_clean",
'language' => "trim|callback_checkbox_validate|xss_clean"
);
$fields = array(
'job_title'=>"Job Title",
'category'=>"Category",
'skill' => "Skills",
'language' => "Language"
);
$this->validation->set_rules($rules);
$this->validation->set_fields($fields);
$this->validation->set_error_delimiters('<div class="error">', '</div>');
/** Run validation on form submitted **/
if ($this->validation->run() == FALSE)
{
$data['category_array'] = array(
'Select one:'=>"Select one:",
'Computer/Network Security'=>"Computer/Network Security",
'Database Development/Administration'=>"Database Development/Administration",
'Desktop Service and Support'=>"Desktop Service and Support"
);
$data['skill_array'] = array(
'PHP', 'ASP', 'ASP.NET', 'Ruby On Rails'
);
$data['language_array'] = array(
'English', 'Spanish', 'French', 'Swahili', 'Kirundi', 'Russian', 'Tagalog', 'American Sign Language'
);
$data['pageinfo'] = "<br /><br />" .
$this->load->view('row_styles/fullpage_1', '', TRUE) .
$this->load->view('grid/grid_add', $data, TRUE) .
$this->load->view('row_styles/fullpage_2', '', TRUE);
$data['pageinfo'] .= "<br /> ";
$data['head_title'] = 'Add Job';
$data['navigation'] = $this->load->view('custom_template_data/navigation_job_add', '', TRUE);
$this->template->load('mainTemplate', 'common/blank', $data);
}
else
{
$this->Job_model->addJob();
redirect('job/view/', 'location');
}
}
}
View:
Code:
<?php echo form_open('grid/add', array('name'=>'myform')); ?>
<?php echo form_hidden('addedby', '1115'); ?>
<table border=1 width=750>
<tr>
<th colspan=2>Job Detail [<?php echo anchor('job/view', 'X'); ?>]</th>
</tr>
<tr valign=top>
<td width=15%>Job Title</td>
<td>
<?php echo $this->validation->error('job_title'); ?>
<?php echo form_input(array('name'=>'job_title', 'id'=>'job_title', 'size'=>'50', 'value'=>'')); ?>
</td>
</tr>
<tr valign=top>
<td width=15%>Category</td>
<td>
<?php echo $this->validation->error('category'); ?>
<?php echo form_dropdown('category', $category_array, ($this->input->post('category') ? $this->input->post('category') : 'Select one:')); ?>
</td>
</tr>
<tr valign=top>
<td width=15%>Skills</td>
<td>
<?php echo $this->validation->error('skill'); ?>
<?php echo form_checkbox('skill', $skill_array, ($this->input->post('skill') ? $this->input->post('skill') : NULL), '', array('type'=>'button')); ?>
</td>
</tr>
<tr valign=top>
<td width=15%>Language</td>
<td>
<?php echo $this->validation->error('language'); ?>
<?php echo form_checkbox('language', $language_array, ($this->input->post('language') ? $this->input->post('language') : NULL), '', array('type'=>'checkbox')); ?>
</td>
</tr>
<tr valign=top>
<td width=15%> </td>
<td><input name="submit" type="submit" id="submit" value="Submit" style="font-family:Verdana; font-size:8pt; font-weight:bold; color:#FFFFFF; height:20; background:#547363; border:1 solid #808080; cursor:hand"></td>
</tr>
</table>
</form>