Welcome Guest, Not a member yet? Register   Sign In
Form Validation Problem
#1

[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">
        &lt;?php echo validation_errors(); ?&gt;
        &lt;?php foreach($permissions as $row):?&gt;
        <li>
            &lt;?=form_label($row['permission_desc'],'perms[]')?&gt;
            &lt;?=form_checkbox('perms[]',$row['permission_id'],$row['checked'])?&gt;
        </li>
        &lt;?php endforeach;?&gt;
        <li>
        &lt;?=form_submit('','Update')?&gt;
    </ul>
&lt;?=form_fieldset_close()?&gt;
&lt;?=form_close()?&gt;

And results quite happily in this:
Code:
&lt;form action="http://localhost/permissions/edit/1" method="post"&gt;&lt;fieldset>
<legend>Permissions for Test Group</legend>
    <ul id="group-perm-edit-list">
        <li>
            <label for="perms[]">asdf</label>
            &lt;input type="checkbox" name="perms[]" value="1" checked="checked"  /&gt;        
        </li>
        <li>
            <label for="perms[]">asdf</label>
            &lt;input type="checkbox" name="perms[]" value="2" checked="checked"  /&gt;        
        </li>
        <li>
            <label for="perms[]">asdf</label>            
            &lt;input type="checkbox" name="perms[]" value="3" checked="checked"  /&gt;        
        </li>
        <li>
            <label for="perms[]">asdf</label>            
            &lt;input type="checkbox" name="perms[]" value="4"  /&gt;        
        </li>
        <li>
            <label for="perms[]">asdf</label>            
            &lt;input type="checkbox" name="perms[]" value="5"  /&gt;        
        </li>
        <li>
            &lt;input type="submit" name="" value="Update"  /&gt;
        </li>
    </ul>
</fieldset>&lt;/form&gt;

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!
#2

[eluser]sketchynix[/eluser]
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
        {
            //Reload the view here as the validation has failed..
           $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);
        }

You shouldnt be making database calls like this in the controller.. make a method in your model
Code:
//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;

if you want to pm me the code, id be willing to rework it a bit.
#3

[eluser]Unknown[/eluser]
Hi Sketchynix, thanks for helping out;
[quote author="sketchynix" date="1285376709"]
You shouldnt be making database calls like this in the controller.. make a method in your model
[/quote]

You're absolutely right; I hadn't even realised I'd put that call in the controller. Probably the caffeine dulling my brain. Apparently, this was also somehow hiding the error...Meh, I guess I'll chalk this one up to the "Learning Curve" column. Thanks for the offer of reworking, but I'll be ok, I like to do things by rapid prototyping!




Theme © iAndrew 2016 - Forum software by © MyBB