繁体   English   中英

Codeigniter中的表单验证错误

[英]form validation error in Codeigniter

我一直在Tutplus上关注Envato的本教程: 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'
     )
);

}

user.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

    }

}

您可以尝试此操作(在调用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
    }
}

您确保已编写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.....} 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM