Codeigniter中的form_validation出错

时间:2013-11-26 17:26:36

标签: php codeigniter

我在codeigniter中使用以下代码,并且收到错误,因为form_validation没有运行。请帮我解决问题。控制器中的代码是:

    public function event_desc()
{       
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $this->form_validation->set_rules('event_title','Event Title','required|max_length[40]');
    $this->form_validation->set_rules('event_desc','Event Description','required');
    $this->form_validation->set_rules('event_cat','Event Category','required|callback_is_default');
    $this->form_validation->set_rules('event_date','Event Date','required|date');
    $this->form_validation->set_rules('event_venue','Event Venue','required||max_length[25]');
    $this->form_validation->set_rules('event_charges','Event Charges','required');
    if($this->form_validation->run()== FALSE)
    {       
    echo "error";
     $this->is_logged_in();
    }
    else
    {
        echo "success";
        $userId = $this->ion_auth->get_user_id();

            $data=array(
                'cat_id '=> $this->input->post('event_cat'),
                'user_id' => $userId,
                'event_title' => $this->input->post('event_title'),
                'event_desc' => $this->input->post('event_desc'),
                'event_date' => $this->input->post('event_date'),
                'event_venue' => $this->input->post('event_venue'),
                'event_charges' => $this->input->post('event_charges'),
                );
            $this->load->model('event_model');  
            $this->event_model->add_event_record($data);
            $this->index();


    }

}
public function _is_default($str)
{
if ($str != 'yes' && $str != 'no')
{
    $this->form_validation->set_message('_check_select_yes_or_no', 'Please select yes or no for the %s field');
    return FALSE;
}
else
{
    return TRUE;
}
}

当我插入数据时,如果form_validation()运行为false,则提交页面显示“错误”,因为我已经回显“错误”。 我在View中的代码是:

 <?php echo form_open('profile/event_desc'); ?>
        <div class="event_wrapper">
            <div class="label_content">
                <div class="event_details">Event Title</div>
                 <input type="text" name="event_title" id="event_title" />
                 <?php echo form_error('event_title','<div class="error_event">','</div>');?>
            </div><!-- end of label_content-->
            <div class="label_content">
                <div class="event_details">Event Description</div>
                 <textarea name="event_desc" id="event_desc"></textarea>
                 <?php echo form_error('event_desc','<div class="error_event">','</div>')?>
            </div><!-- end of label_content-->
            <div class="label_content">
           <div class="event_details"> Event Category</div>
              <select name="event_cat"  id="eventcat">
                <option value="select">_Select one_</option>
                <option value="1">Informational Technology</option>
                <option value="2">Education</option>
                <option value="3">Entertainment</option>
                <option value="4">Sports</option>
                <option value="5">Seminars</option>
                <option value="6">Research</option>
                <option value="7">Science</option>
                </select>
                <?php echo form_error('event_cat','<div class="error_event">','</div>')?>
            </div><!-- end of label_content-->
            <div class="label_content">
           <div class="event_details">  Event Date</div>
             <input type="datetime" name="event_date" value="" id="event_title"/>
              <?php echo form_error('event_date','<div class="error_event">','</div>')?>
            </div>
             <div class="label_content">
           <div class="event_details">  Event Venue</div>
             <input type="datetime" name="event_venue" value="" id="event_title"/>
              <?php echo form_error('event_venue','<div class="error_event">','</div>')?>
            </div>
              <div class="label_content">
           <div class="event_details">  Event Charges</div>
             <input type="datetime" name="event_charges" value="" id="event_title"/>
              <?php echo form_error('event_charges','<div class="error_event">','</div>')?>
            </div>
             <div class="label_content">
                <div class="submit_gap">
                    <button type="submit" class="signin">Upload</button>
                </div>

请帮我解决错误。

2 个答案:

答案 0 :(得分:0)

根据codeigniter文档 - 尝试将回调从_is_default更改为is_default。

$this->form_validation->set_rules('username', 'Username', 'callback_username_check');


public function username_check($str){ /*code*/ }

答案 1 :(得分:0)

我看了你的密码并发现了以下问题。

  1. 这个规则错了它包含两个|而不是一个。所以正确的规则是。

    $this->form_validation->set_rules('event_venue','Event Venue','required|max_length[25]');
    
  2. 您的回调永远不会成立,因为您不会从选择菜单中返回是或否,因此应该进行正确的回调。

    public function is_default($str)
    {
     if ($str == 'select')
     {
      $this->form_validation->set_message('is_default', 'Please select yes or no for the        %s field');
     return FALSE;
     }
     else
     {
      return TRUE;
      }
     }
    
  3. 将此部分替换为您的代码,它将起作用