无法设置从配置文件codeigniter加载的验证规则

时间:2015-04-16 09:51:25

标签: codeigniter validation

我的配置文件

配置/ login_rules

我在这里定义了我的登录表单验证规则

   <?php


/**
* SETTING VALIDATION RULES FOR THE LOGIN FORM
*/

$config['login_settings'] = array(

        array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required|trim|min_length[6]|max_length[20]|xss_clean',
                'errors' => array(
                        'required' => 'You must provide a %s.',
                ),
        ),

        array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|trim|valid_email|xss_clean'
        )
);


/**
* SETTING ATTRIBUTES FOR THE LOGIN FORM
*/

$config['login_attribute'] = array(

        'form'  => array(

                        'id' => 'loginform',
                        'class' => 'form-horizontal', 
                        'role' => 'form'  
                        ),

        'email'=> array(

                        'id'=>'login-username',
                        'class' => 'form-control',
                        'name'=>'email',
                        'placeholder' => 'Enter Email',
                        'value'=>set_value('email')
                        ),

        'password' =>array(

                        'id'=>'login-password',
                        'class' => 'form-control',
                        'name'=>'password',
                        'placeholder'=>'Enter Password'

                        ),

        'checkbox' =>array(

                       'id'          => 'login-remember',
                       'class'       => 'form-control',
                       'name'        => 'remember_me',
                       'value'       => '1',
                       'checked'     => TRUE
                        ),

        'submit' =>array(

                       'id'          => 'btn-login',
                       'class'       => 'btn btn-success',
                       'name'        => 'submit',
                       'value'       => 'Login'

                      )
           );

?>

我的控制器(Login.php)

在我的控制器中,我正在加载配置文件,尝试为其应用验证规则

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class login extends CI_Controller {


        public function __construct() 
        {        

             parent::__construct();

             //$this->output->enable_profiler(TRUE);
        }

        public function index()
        {

            echo 'login controller index fun';
        }

        public function login()
        {

       $this->config->load('login_rules');

       $this->form_validation->set_rules($this->config->item('login_settings'));

       $data["login_attrib"] = $this->config->item("login_attribute");


            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('login_form',$data["login_attrib"]);
            }
            else
            {

                echo 'Success';
            }
        }

}

?>

我没有收到任何错误,验证规则也没有工作

查看文件(login_form.php)

  <?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<html>
<head>
<title>Admin Login</title>
</head>
<body>

<?php echo validation_errors(); 

echo form_open('login/login',$form);
echo form_input($email);
echo form_input($password);

echo form_submit($submit);
echo form_close();

?>

</body>
</html>

1 个答案:

答案 0 :(得分:4)

试试这个。

$this->config->load('login_rules');

$this->form_validation->set_rules($this->config->item('login_settings'));

对你有用。