0

我一直在关注 Envato 在 Tutplus 上的本教程:https ://tutsplus.com/course/build-a-cms-in-codeigniter/ 。我正在验证登录表单的部分。我的问题是我不能在登录系统中使用我的表单验证规则。我的代码如下:

user_m.php

<?php 
  class User_M extends MY_Model {

public $rules = array(  // I can't use this rules in my controller
   'email' => array(  // for email 
    'field' => 'email',
    'label' => 'Email',
    'rule'  => 'trim|required|valid_email|xss_clean'
    ),
    'password' => array(  // for password
    'field' => 'password',
    'label' => 'Password',
    'rule'  => 'trim|required'
     )
);

}

用户.php

 <?php 

class User extends Admin_Controller {

public function __construct()
{
    parent::__construct();
}

public function login()
{   
   // Set form
   $rules = $this->user_m->rules; // get the value from user_m model and it works well
       // this is not works. this is my problem
   $this->form_validation->set_rules($rules);  

       // if we use this comment code then it works
   //$this->form_validation->set_rules('email', 'Email', 'rim|required|valid_email|xss_clean'); 
   // $this->form_validation->set_rules('password', 'Password', 'required');

   // Process form
   if ( $this->form_validation->run() == TRUE )  { // show the error msg if form problem occurs 
       // We can login and redirect

    }

}
4

2 回答 2

0

form_validation->run()你可以试试这个(在你调用函数之前在你的控制器中设置规则)

public function login()
{
    $this->form_validation->set_rules('email', 'Email',  'trim|required|valid_email|xss_clean');
    $this->form_validation->set_rules('password', 'Password',  'trim|required');
    if ( $this->form_validation->run() )  {
        // validation passed
    }
    else {
        // validation failed
    }
}
于 2013-10-19T23:06:25.717 回答
0

你确保你已经写了 form_error('email'); form_error('password'); 在电子邮件和密码文本框下方。如果发生验证错误,请调用登录视图。

public function login()
{
   $this->form_validation->set_rules('email', 'Email',  'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('password', 'Password',  'trim|required');
   if ($this->form_validation->run() == FALSE)
        {
           **$this->load->view('login view path');**
        }else{write other code of redirect or any.....} 
}
于 2013-10-26T13:22:08.567 回答