[eluser]Unknown[/eluser]
Hey all,
I'm new to Code Igniter, and I really like it so far. I've just run into a strange problem which I can't seem to solve; it's not "mission-critical" or anything, but it IS infuriating me!
I've a got a form, created with the form helper, and I'm using the form_validation library to validate it.
My problem is this: my form is basically an array of checkboxes, named 'perms[]'. The form is created without incident. I don't want users to be able to select NO checkboxes, so I add a rule in the controller that says perms[] is required. This works fine. The rule executes and people cannot submit an empty array. BUT the error accompanying a fail NEVER shows. I've tried using validation_errors(), form_error(), in the view and controller. I've tried with and without custom messages, I've tried using a callback function instead, I've tried with and without square brackets in the name when I set the rules.
I don't understand. I have a login form that works fine, I can't see what I've done.
The problem form is loaded from this view:
Code:
<?=form_open(uri_string())?>
<?=form_fieldset("Permissions for $group_name")?>
<ul id="group-perm-edit-list">
<?php echo validation_errors(); ?>
<?php foreach($permissions as $row):?>
<li>
<?=form_label($row['permission_desc'],'perms[]')?>
<?=form_checkbox('perms[]',$row['permission_id'],$row['checked'])?>
</li>
<?php endforeach;?>
<li>
<?=form_submit('','Update')?>
</ul>
<?=form_fieldset_close()?>
<?=form_close()?>
And results quite happily in this:
Code:
<form action="http://localhost/permissions/edit/1" method="post"><fieldset>
<legend>Permissions for Test Group</legend>
<ul id="group-perm-edit-list">
<li>
<label for="perms[]">asdf</label>
<input type="checkbox" name="perms[]" value="1" checked="checked" />
</li>
<li>
<label for="perms[]">asdf</label>
<input type="checkbox" name="perms[]" value="2" checked="checked" />
</li>
<li>
<label for="perms[]">asdf</label>
<input type="checkbox" name="perms[]" value="3" checked="checked" />
</li>
<li>
<label for="perms[]">asdf</label>
<input type="checkbox" name="perms[]" value="4" />
</li>
<li>
<label for="perms[]">asdf</label>
<input type="checkbox" name="perms[]" value="5" />
</li>
<li>
<input type="submit" name="" value="Update" />
</li>
</ul>
</fieldset></form>
The controller function responsible is here:
Code:
function edit($group_id)
{
$this->form_validation->set_message('required',"You cannot have a group with no permissions.");
$this->form_validation->set_rules('perms[]','Permissions','trim|required');
if($this->form_validation->run())
{
//The form has been submitted,
//Process the results.
echo "DONE!";
}
else
{
echo "AAAAH!";
}
//Firstly check that the group id exists.
$this->db->where("group_id",$group_id);
$query = $this->db->get("groups");
if ($query->num_rows() > 0)
{
$group = $query->row(0);
$name = $group->group_name;
$permissions = $this->permissions_model->edit_group_perms($group_id);
$options = array("group_name"=>$name,"permissions"=>$permissions);
$this->load->view("permissions/single_group_edit_form",$options);
}
}
Much obliged for your time!