0

大家好,我让我的代码更简单只是为了显示错误消息,但现在仍然显示。

 public function check_profile_ifexist($id)
  {

    if($this->input->post('edit')){

    $this->form_validation->set_rules('email','Email','trim|valid_email|is_unique[user_details.email]'); 
    $this->form_validation->set_rules('username','Username','trim|min_length[6]|max_length[20]|is_unique[user_details.username]|xss_clean');                $this->form_validation->set_message('is_unique',"That %s already exists!");
    $this->form_validation->set_message('max_length', 'the maximum characters for %s is 20');
    $this->form_validation->set_message('min_length', 'the minimum characters for %s is 6');

    if ($this->form_validation->run())
    {
        $this->load->model('model_user_manage');
        $em=$this->model_user_manage->update_all($id);
    }
    else
    {

     $this->view_profile($id);  
     }

}

}

4

1 回答 1

0

与其回显所有错误,不如将它们全部存储到一个属性中:

首先启动一个空属性,以便您知道它存在 - 您可以在 check_profile_ifexist() 方法的开头执行此操作:

 $this->profile_errors = '';

然后在您的 check_profile_ifexist() 方法中,只需将错误添加到属性中,而不是回显它们,例如

if (MD5($username)==$pass){
    echo "username cannot be the same as password";
}

变成:

if (MD5($username)==$pass){
     $this->profile_errors .= "username cannot be the same as password\n";
}

然后,此属性将可用于您的所有方法,因此您可以将其添加到 view_profile() 中的数据数组中,如下所示:

if(isset($this->profile_errors)){
    $data['errors'] = $this->profile_errors;
}else{
    $data['errors'] = ''; 
}

并且错误在您的视图中显示为 $errors,因此您可以echo $errors;打印出所有错误。

注意显然,您必须确保该属性始终存在或检查其存在以避免您自己的错误。我还在您的错误中添加了换行符,以便在批量打印多个时它们看起来更整洁。

第二个注释你似乎有很多东西我会在这个代码中放入一个模型,我认为它是一个控制器。您可能应该将所有数据库内容保存在模型中,否则 MVC 警察会来找您。

于 2013-01-07T04:27:20.353 回答