Welcome Guest, Not a member yet? Register   Sign In
Re-populating checkboxes -- callback works great, but "selected" doesn't happen
#1

[eluser]tinawina[/eluser]
I understand that CI doesn't do a good job of checkbox validation out of the box. I got around that by writing a custom callback that does the trick. But what I don't understand is why CI's re-populate checkboxes -- as described in the CI validation class tutorial -- doesn't work.

Right now I have a form that is just a little testbed to work this all out. The form has 4 checkboxes. I want to make sure that at least one box is checked, and no more than 3 are checked. Here's my form:

Code:
<form action="add" method="post">
<?=$this->validation->box_error;?>
<input type="checkbox" name="box[]" value="10" <?= $this->validation->set_checkbox('box', '10'); ?> />
<input type="checkbox" name="box[]" value="20" <?= $this->validation->set_checkbox('box', '20'); ?> />
<input type="checkbox" name="box[]" value="30" <?= $this->validation->set_checkbox('box', '30'); ?> />
<input type="checkbox" name="box[]" value="40" <?= $this->validation->set_checkbox('box', '40'); ?> />
<input type="submit" name="submit" value="submit" />
</form>

On the backend, I have this method in my class to handle the submitted form (Note: I am using joeles' callback library and revised validation class):

Code:
function add()
{
    $this->load->helper(array('form', 'url'));
        $this->load->library('validation');
        $this->load->library('callbacks');
    $this->validation->callback_obj =& $this->callbacks;

    $rules['box']    = "required|callback__boxCheck";
    $fields['box']    = "Box";

    $this->validation->set_rules($rules);
    $this->validation->set_fields($fields);

    $this->validation->set_error_delimiters('<span class="error">', '</span>');

    if ($this->validation->run() === false)
    {
        $this->view->load('forms/test');
    }
    else
    {
        echo 'done!';
        print_r($this->input->post('box'));
    }
}

And I have this callback

Code:
function _boxCheck ()
{
    $how_many = count($this->CI->input->post('box'));
    if ( ($how_many < '1') || ($how_many > '3') )
    {
        $this->CI->validation->set_message('_boxCheck', 'Choose at least 1 and no more than 3 boxes.');
        return false;
    }
    else
    {
        return true;
    }
}

When I submit the form with nothing checked, or with all four boxes checked, I get the expected result -- the form is reposted and I see my "Choose at least 1...no more than 3." error message. But when I select all four boxes and submit, I get my error message but none of the previously selected boxes are checked on form re-post.

I am hopeful that I've missed something small but crucial (happens to me a LOT). I did see that there's a revised validation class I can use that handles checkboxes efficiently. I was hoping to not mess with the validation class I am using now since it is a custom version and I'd have to merge the two versions (ugh) to get everything working the way I need it to.

Thanks for any help offered here!




Theme © iAndrew 2016 - Forum software by © MyBB