Welcome Guest, Not a member yet? Register   Sign In
Creating validation for a checkbox array.
#1

[eluser]Bramme[/eluser]
Okay, in my form I have a series of checkboxes called categories[] so I can process them as an array in my controller.

However, there's no validation for this in CI 1.6.3 (is there in 1.7? if so, i'm upgrading) so I thought I'd write my own.

Code:
$rules['categories[]'] = 'callback_required_checkbox';
That's in my controller

and in MY_Validation.php I have
Code:
function required_checkbox($str) {

    $ci =& get_instance();
    
    $ci->validation->set_message('require_array_checkbox', 'You have to set at least one checkbox');
        
    if( $ci->input->post($str) == '') {
        return FALSE;
    } else {
        return TRUE;
    }
}
However, nothing happens. I'd even be happy with an error message, but not even that! Unless I delete a ; offcourse. If I try to echo something in it, nothing happens either...

Can anybody help me out?
#2

[eluser]xwero[/eluser]
The new Form_validation library makes it possible to use arrays but it's still a little rough around the edges.

In the current validation library you can do
Code:
$this->validation->set_message('isset','You have to set at least one checkbox');
$rules['categories'] = 'isset';
#3

[eluser]Bramme[/eluser]
okay, I'm implementing that tonight :p

Can't access the svn folder from my work atm... Stupid proxy.

Did they include something to prepolute the checkboxes? Because my hacks are rather ugly at the moment. It seems to be working though.

This is smth from my controller:
Code:
if( $this->input->post('categories') != FALSE) {
    $data['checkbox_array'] = $this->input->post('categories');
} else {
    $this->db->where('tutID', $tutID);
    $tutcats_query = $this->db->get('tutorial_categories');
    foreach($tutcats_query->result() as $row) {
        $selected_cats[] = $row->catID;
    }
    $data['checkbox_array'] = $selected_cats;
}
in my view i've got
Code:
<input type="checkbox" name="categories[]" value="<?=$cat['catID']?>" id="cat-<?=$cat['catID']?>" <?php echo $this->validation->set_checkbox_array($cat['catID'], $checkbox_array); ?>/>
And in MY_Validation.php
Code:
function set_checkbox_array($needle = '', $haystack = array())
{
    if(empty($needle) OR empty($haystack))
    {
        return FAlSE;
    }
    else if (in_array($needle, $haystack))
    {
        return 'checked="checked"';
    }
    else
    {
        return FAlSE;
    }
}
#4

[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 />&nbsp;";
                $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:
&lt;?php echo form_open('grid/add', array('name'=>'myform')); ?&gt;
&lt;?php echo form_hidden('addedby', '1115'); ?&gt;

<table border=1 width=750>
    
    <tr>
        <th colspan=2>Job Detail [&lt;?php echo anchor('job/view', 'X'); ?&gt;]</th>
    </tr>
    
    <tr valign=top>
        <td width=15%>Job Title</td>
        <td>
            &lt;?php echo $this->validation->error('job_title'); ?&gt;
            
            &lt;?php echo form_input(array('name'=>'job_title', 'id'=>'job_title', 'size'=>'50', 'value'=>'')); ?&gt;
        </td>
    </tr>
    
    <tr valign=top>
        <td width=15%>Category</td>
        <td>
            &lt;?php echo $this->validation->error('category'); ?&gt;
            &lt;?php echo form_dropdown('category', $category_array, ($this->input->post('category') ? $this->input->post('category') : 'Select one:')); ?&gt;            
        </td>
    </tr>
    
    <tr valign=top>
        <td width=15%>Skills</td>
        <td>
            &lt;?php echo $this->validation->error('skill'); ?&gt;
            &lt;?php echo form_checkbox('skill', $skill_array, ($this->input->post('skill') ? $this->input->post('skill') : NULL), '', array('type'=>'button')); ?&gt;            
        </td>
    </tr>
    
    <tr valign=top>
        <td width=15%>Language</td>
        <td>
            &lt;?php echo $this->validation->error('language'); ?&gt;
            &lt;?php echo form_checkbox('language', $language_array, ($this->input->post('language') ? $this->input->post('language') : NULL), '', array('type'=>'checkbox')); ?&gt;
        </td>
    </tr>    
    
    <tr valign=top>
        <td width=15%>&nbsp;</td>
        <td>&lt;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"&gt;&lt;/td>
    </tr>
    
</table>

&lt;/form&gt;




Theme © iAndrew 2016 - Forum software by © MyBB