【问题标题】:CodeIgniter form_validation->run() always returns false?CodeIgniter form_validation->run() 总是返回false?
【发布时间】:2013-06-08 15:03:29
【问题描述】:

我是CodeIgniter 的新手,我一直在尝试实现表单提交功能,但是每当我按下“提交”时,表单页面都会刷新并且数据库不会更新!似乎$this->form_validation->run() 总是返回false,但我不知道为什么。

控制器功能如下:

public function write_prof_review($prof_id)
    {
        $this->load->model('Queries');
        // form stuff here
        $this->load->helper('form');
        $this->load->library('form_validation');

        $data['prof_id'] = $prof_id;
        if($this->form_validation->run() == FALSE){
            $this->load->view('create_prof_review', $data);
        }
        else {
            $this->Queries->submit_prof_review($prof_id, $this->USERID);
            $this->load->view('form_submitted');
        }       
    }

这是模型中的函数 submit_prof_review():

function submit_prof_review($prof_id, $user_id)
    {
        $data = array(
            'course_code' => $this->input->post('course_code'),
            'easiness' => $this->input->post('easiness'),
            'helpfulness' => $this->input->post('helpfulness'),
            'clarity' => $this->input->post('clarity'),
            'comment' => $this->input->post('comment')
        );

    $average = round((($data['easiness'] + $data['helpfulness'] + $data['clarity'])/3),2);
    date_default_timezone_set('Asia/Hong_Kong');
    $date = date('m/d/Y h:i:s a', time());

    $data['average'] = $average;
    $data['date'] = $date;
    $data['course_id'] = 0; 

    return $this->db->insert('review_prof', $data);
}

最后是表单的视图(create_prof_review.php):

<h2>Write a review</h2>
<?php echo form_open('home/write_prof_review/'.$prof_id); ?>
<h3>Course code
<input type = 'text' name = 'course_code'></h3>
<h3>Easiness
<input type = 'text' name = 'easiness'></h3>
<h3>Helpfulness
<input type = 'text' name = 'helpfulness'></h3>
<h3>Clarity
<input type = 'text' name = 'clarity'></h3>
<h3>Comment</h3>
<textarea name = 'comment' rows = '4' cols = '50'></textarea>
<br>
<input type = 'submit' name = 'submit' value = 'Submit'>
</form>

被困在这个问题上几天了,但我仍然无法弄清楚出了什么问题。任何帮助将不胜感激!

【问题讨论】:

  • 就我而言,这是一个愚蠢的错误。我没有在任何表单控件 上使用name 属性

标签: php codeigniter codeigniter-form-helper


【解决方案1】:

虽然原因不同,但我遇到了同样的问题。我错过了我从表单中验证的输入字段之一,即

private function validate_data(){
        $validate_data = array(

            array(

                'field' => 'steps',
                'label' => 'Steps',
                'rules' => 'trim|required|integer|xss_clean'

            ),

            array(

                'field' => 'pace',
                'label' => 'Pace',
                'rules' => 'trim|required|integer|xss_clean'

            ),

            array(

                'field' => 'speed',
                'label' => 'Speed',
                'rules' => 'trim|required|numeric|xss_clean'

            ),

            array(

                'field' => 'distance',
                'label' => 'Distance',
                'rules' => 'trim|required|numeric|xss_clean'

            )

        );//end array validate_data

        return $validate_data;
  }

我错过了表单中的速度输入字段。我添加了它,问题就解决了。这真的让我很头疼,因为我只是重用了我用过很多次的代码,所以我不明白为什么($this-&gt;form_validation-&gt;run() 会返回 false,这是我以前从未经历过的。

【讨论】:

  • 我刚刚遇到了类似的情况 - 打开或关闭字段的条件设置不正确,因此该字段正在尝试验证,尽管提交它的视图中不存在。您的回答帮助我回过头来思考发生了什么,所以谢谢!
【解决方案2】:

你可以去system/library/Form_validation.php

if (count($this->_config_rules) == 0)
{
return FALSE;
}

把假改成真

【讨论】:

  • 这是我读过的最糟糕的答案!为什么要编辑系统文件并引入错误??
  • 赞成。这实际上是一个非常好的解决方案。默认情况下,如果未设置任何规则并且无论如何验证表单,CodeIgniter 将返回 FALSE。然而,在大多数(所有?)情况下,这是不受欢迎的行为。此评论的答案解决了这个问题。
  • 两年过去了——@ChristianGiupponi 是对的,这是有史以来最糟糕的答案。永远,无论框架/CMS/无论如何,编辑核心文件。使用您自己的文件进行扩展。
【解决方案3】:

我认为这是因为您没有设置任何验证规则。 控制器代码应如下所示:

public function write_prof_review($prof_id)
{
    $this->load->model('Queries');
    // form stuff here
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['prof_id'] = $prof_id;

    // here it is; I am binding rules 

    $this->form_validation->set_rules('course_code', 'Course Code', 'required');
    $this->form_validation->set_rules('easiness', 'easiness', 'required');
    $this->form_validation->set_rules('helpfulness', 'helpfulness', 'required');

    if($this->form_validation->run() == FALSE) {
        $this->load->view('create_prof_review', $data);
    }
    else {
        $this->Queries->submit_prof_review($prof_id, $this->USERID);
        $this->load->view('form_submitted');
    }
}

请参考the CodeIgniter user guide;它将为您提供有关验证规则的更多信息。

【讨论】:

  • 谢谢,这成功了!然而,在成功提交表单后,我发现了 php date() 函数的一个新问题,该函数在 mysql 数据库中显示为 0000-00-00 00:00:00。无论如何,再次感谢您帮助发布表单!
  • 原来我为 date() 函数使用了错误的日期格式,通过将其更改为 date('Y-m-d H:i:s', time()) 来修复它
  • 谢谢。经过这么多小时的调试,它可以工作了。我有一个 switch case 表单验证规则。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2023-03-09
  • 2021-01-19
  • 2015-12-10
相关资源
最近更新 更多