Codeigniter表单验证问题

时间:2010-04-14 19:41:34

标签: codeigniter

请请有人帮助我

$this->load->library('form_validation');
  $this->load->helper('cookie');

  $data = array();


  if($_POST) { 
   // Set validation rules including additional validation for uniqueness
   $this->form_validation->set_rules('yourname', 'Your Name', 'trim|required');
   $this->form_validation->set_rules('youremail', 'Your Email', 'trim|required|valid_email');
   $this->form_validation->set_rules('friendname', 'Friends Name', 'trim|required');
   $this->form_validation->set_rules('friendemail', 'Friends Email', 'trim|required|valid_email');

   // Run the validation and take action
   if($this->form_validation->run()) { 
    echo 'valid;
   }
  }
  else{
   echo 'problem';
  }

表单验证即将返回,没有错误可以让人知道为什么?

2 个答案:

答案 0 :(得分:1)

它真的回应'有效'吗? (你在那里错过了一个撇号,顺便说一句)

当$ _POST为false时,您显示的代码只会回显'问题',而不是在验证失败时。 不知道更多,它可能很简单:

// Run the validation and take action
if($this->form_validation->run()) { 
 echo('valid');
} else {
 echo('invalid');
}

答案 1 :(得分:0)

尝试这样不用检查是否设置了$ _POST - 不是真的需要:

//validation rules here
//
if ($this->form_validation->run() == TRUE) {
     //do whatever that shall be run on succeed
} else {
     $this->load->view('form'); //load the form
}

详细了解控制器部件here