Welcome Guest, Not a member yet? Register   Sign In
Creating validation for a checkbox array.
#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;


Messages In This Thread
Creating validation for a checkbox array. - by El Forum - 09-12-2008, 06:04 AM
Creating validation for a checkbox array. - by El Forum - 09-12-2008, 06:20 AM
Creating validation for a checkbox array. - by El Forum - 09-12-2008, 06:38 AM
Creating validation for a checkbox array. - by El Forum - 09-26-2008, 12:37 PM



Theme © iAndrew 2016 - Forum software by © MyBB