在codeigniter

时间:2015-11-07 06:23:18

标签: php codeigniter validation

我尝试使用各种方法为多选下拉列表使用codeigniter设置表单验证,但无法正确设置规则。在这下面是我正在使用的代码,请帮助。附:我已经尝试在stackoverflow上实现代码,但是徒劳无功。

<select class="" name="travel_cat[]" multiple="multiple" id="travel_lst" data-placeholder="Select Travel Categories" style="width: 100%;">
                  <option value="adventure">Adventure</option>

                      <option value="arts&culture">Arts & Culture</option>

                      <option value="events">Events</option>

                      <option value="backpacking">Backpacking</option>

                      <option value="beach_holidays">Beach Holidays</option>

                    <option value="budget_travel">Budget Travel</option>

                    <option value="city_travels">City Travels</option>

                    <option value="day_trips">Day Trips</option>

                    <option value="honeymoons">Honeymoons</option>

                    <option value="family_trips">Family Trips</option>

                    <option value="weekend_gateway">Weekend Gateway</option>

                    <option value="history&architecture">History & Architecture</option>

                    <option value="luxury">Luxury</option>

                    <option value="nature">Nature</option>

                    <option value="road_trips">Road Trips</option>

                    <option value="wildlife">Wildlife</option>

                    <option value="religious">Religious</option>
                </select>  

$choice = $this->input->post("travel_cat");
    if(is_null($choice))
    {
        $choice = array();
    }
    $travel_cat = implode(',', $choice);

表单验证: -

$this->form_validation->set_rules('travel_cat', 'Travel Category', 'callback_check_default');

function check_default($array)
{
    foreach($array as $element)
    {
        if($element == '0')
        { 
            return FALSE;
        }
    }
    return TRUE;        
}

1 个答案:

答案 0 :(得分:2)

我已经用下面提到的代码替换了回调函数,并且它工作正常。

function check_default()
{
    $choice = $this->input->post("travel_cat");
    if(is_null($choice))
    {
        $choice = array();
    }
    $travel_cat = implode(',', $choice);

    if($travel_cat != '')
        return true;
    else
        return false;   
}