php CodeIgniter form_validation->run() 总是返回 false?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17000803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CodeIgniter form_validation->run() always returns false?
提问by Cloud Strife
I'm new to CodeIgniter
and I've been trying to implement a form submitting function, however whenever I press "submit" the form page simply refreshes and the database is not updated! It seems that the $this->form_validation->run()
is always returning false, but I have no idea why.
我是新手CodeIgniter
,我一直在尝试实现表单提交功能,但是每当我按下“提交”时,表单页面都会刷新并且数据库不会更新!似乎$this->form_validation->run()
总是返回false,但我不知道为什么。
The controllerfunction is as follows:
所述控制器的功能如下:
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');
}
}
And here is the function submit_prof_review() in the model:
这里是功能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);
}
And finally the view for the form (create_prof_review.php):
最后是表单的视图(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>
Been stuck on this for a couple of days, but I still can't figure out what's wrong. Any help would be greatly appreciated!
坚持了几天,但我仍然无法弄清楚出了什么问题。任何帮助将不胜感激!
回答by nana.chorage
I think this is happening because you have not set any validation rules. Controller code should look like this:
我认为这是因为您没有设置任何验证规则。控制器代码应如下所示:
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');
}
}
Please refer to the CodeIgniter user guide; it will give you more information about validation rules.
请参阅CodeIgniter 用户指南;它将为您提供有关验证规则的更多信息。
回答by theTypan
I had the same problem though the cause was different. I was missing one of the input fields that I was validating from the form i.e
虽然原因不同,但我遇到了同样的问题。我错过了我正在从表单验证的输入字段之一
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;
}
I was missing the speed input field in the form. I added it and the problem was solved. It really gave me a headache cause I was just reusing code that I have used so many times, so I could not understand why ($this->form_validation->run()
was returning false, something that I had never experienced before.
我缺少表单中的速度输入字段。我添加了它,问题就解决了。这真的让我头疼,因为我只是在重复使用我使用过很多次的代码,所以我不明白为什么($this->form_validation->run()
返回 false,这是我以前从未经历过的。
回答by Ahmed Shaaban Elgendy
You can go to system/library/Form_validation.php
你可以去 system/library/Form_validation.php
and in
并在
if (count($this->_config_rules) == 0)
{
return FALSE;
}
change false to true
把假改成真