表单验证:无法访问与您的字段名称密码对应的错误消息

时间:2015-06-15 11:39:54

标签: ajax forms codeigniter validation authentication

我在codeigniter网站的firebug中收到此错误 无法访问与您的字段名称密码对应的错误消息 我已经搜索过,发现当代码中有错误时会弹出错误,但是没有像我这样的问题。 你能帮忙吗?

登录控制器

function do_login()
{
 $username = $this->input->post('username');
 $password = $this->input->post('password');

 if(!empty($username) && isset($username))
 {
 $this->form_validation->set_rules(
      'username', 'Your Username',
      'trim|required|xss_clean|max_length[20]|callback_username_check');

 $this->form_validation->set_rules('password', 'Password','trim|required||xss_clean|callback_password_check['.$username.']|min_length[4]|max_length[20]');

if($this->form_validation->run() == FALSE)
{

 echo json_encode(array('st'=>0, 'msg' => validation_errors()));
}
 else if(($this->form_validation->run() == TRUE))
{ 
echo json_encode(array('st'=>1));
}
function username_check($username)
{
$this->form_validation->set_message('username_check', ' is not regesterd!');
$checkVar=$this->user_model->check_user($username);
if($checkVar == true)
{return TRUE;}
elseif ($checkVar == false)
{return false;}
}   
function password_check($username,$password)
{
$this->form_validation->set_message('password_check', ' is not correct!');
$checkVar =  $this->user_model->check_pass($username,$password);
if($checkVar == true)
{return TRUE;}
elseif ($checkVar == false)
{return false;
}}}}

user_model

public function check_user($username) 
{
$this->query = $this->db->select('COUNT(*)')
    ->from('users')
    ->where(array('username'=>$username))
    ->limit(1)->get();
$query = $this->query->num_rows();
if ($query> 0){
    return true;
}
else{
    return false;
}
}
 public function check_pass($username, $password) 
{
$this->query = $this->db->select('COUNT(*)')
    ->from('users')
    ->where(array('username'=>$username, $password))
    ->limit(1)->get();
$query = $this->query->num_rows();
if ($query> 0){
    return true;
}
else{
    return false;
}
}

AJAX

<script type="text/javascript">
$(document).ready(function() {
$('#frm').submit(function(){

$.post(
     $('#frm').attr('action'), 
     $('#frm').serialize(), 
     function( data ) 
     {
     if (data.st == 0)
     {
     $('#validation-error').html(data.msg);
     }
     else if(data.st == 1)
     {           var url = "home/"
     window.location.href = url;

     }
     }, 
     'json'
   );
 return false;   
  });


});

</script>

形式

<?php echo form_open('signin/do_login', array('id'=>'frm')); ?>

                                <input type="text" id='username' name="username" size="15" placeholder="اسم المستخدم">                              

                                                                    <input type="password" id="password" name="password" size="15" placeholder="كلمة المرور" >                              
                                                                        <div id="validation-error"></div>
                            <input type='submit' value="تسجيل الدخول">
                                        </form>

2 个答案:

答案 0 :(得分:3)

搜索和调试后我发现问题出现在这一行

 $this->form_validation->set_rules('password', 'Password','trim|required||xss_clean|callback_password_check['.$username.']|min_length[4]|max_length[20]');

应该是

 $this->form_validation->set_rules('password', 'Password','trim|required|xss_clean|callback_password_check['.$username.']|min_length[4]|max_length[20]');

问题是&#34; | &#34;介于必需和xss之间 谢谢

答案 1 :(得分:2)

你在控制器

中失去了方括号
'trim|required||xss_clean|callback_password_check['.$username.'|min_length[4]|max_length[20]'

应该是

'trim|required||xss_clean|callback_password_check['.$username.']|min_length[4]|max_length[20]'