【问题标题】:codeigniter form_validation returning falsecodeigniter form_validation 返回 false
【发布时间】:2016-10-09 12:50:57
【问题描述】:

谁能帮助我为什么我的代码返回 if ($this->form_validation->run() == FALSE) 。我是codeigniter的初学者,我想使用form_validation来匹配更改密码的规则。我只想将语句 form_validation 返回为 true,以便我可以继续在数据库中进行更新。

我已经加载了 form_validation、url 等。 我的 callback_oldpassword 代码工作正常,所以我不需要在我的问题中涉及它。所以别管它已经返回 true 的回调了。

我想要的是专注于我的密码匹配?我认为这是错误的做法。谁能帮我看看会发生什么。 ?根据我声明的规则将 form_validation 返回为 true。

这是我的表格:

<form id="pass_form" class="form-horizontal">

    <?php $userid = ($this->session->userdata['logged_in']['user_id']); ?>
                                                        <input type="hidden" name="userid" id="userid" value="<?php echo $userid ?>" />

    <div class="form-group">
    <label class="control-label col-sm-3" for="curPword">Current Password</label>
    <div class="col-sm-6">
    <input type="password" class="form-control" name="curPword" id="curPword" placeholder="Enter current password" required />
    </div>
    </div>

    <div class="form-group">
    <label class="control-label col-sm-3" for="newPword">New Password</label>
    <div class="col-sm-6">
    <input type="password" class="form-control" name="newPword" id="newPword" placeholder="Enter new password" required />
    </div>
    </div>

    <div class="form-group">
    <label class="control-label col-sm-3" for="confPword">Confirm Password</label>
    <div class="col-sm-6">
    <input type="password" class="form-control" name="confPword" id="confPword" placeholder="Confirm new password" required />
    </div>
    <div class="col-sm-8 ajax_pass_result"></div>
    </div>
    <button onclick="changepass(event)" class="btn btn-success btn-sm "><i class="fa fa-refresh"></i> Update Password</button>
</form>

我的 ajax:

function changepass(e) {
    e.preventDefault();
    jQuery.ajax({
    type: "POST",
    url:  "<?php echo site_url('manager/profile/update_password') ?>",    
    data: $("#pass_form").serialize(),
    success: function(res) {
        $(".ajax_pass_result").html(res);

     }
    });

}

和我的控制器,其中 form_validation 总是返回 false:

public function update_password() {


        $config = array(
            array(
                'field'   => 'curPword',
                'label'   => 'curPword',
                'rules'   => 'trim|required|callback_oldpassword_check' // Note: Notice added callback verifier.
            ),
            array(
                'field'   => 'confPword',
                'label'   => 'confPword',
                'rules'   => 'trim|required|matches[password]'
            ),
            array(
                'field'   => 'newPword',
                'label'   => 'newPword',
                'rules'   => 'trim|required'
            ));
        $this->form_validation->set_rules($config);

         if ($this->form_validation->run() == FALSE) {
               echo 'Error in password fields !';  
         }
         else {
              unset($_POST);
              $message = "No Error ";
              echo "<script type='text/javascript'>alert('$message');</script>";
          }
  }

【问题讨论】:

  • 你需要使用 鉴于这会告诉你你得到了什么。
  • 您陷入了使用压缩词的陷阱(因为缺少更好的术语),然后又退回到使用完整的词...在您的规则中,您有条目匹配 [密码] ,密码在哪里?它不应该是 curPword 或 currentPassword,读起来好多了。完整拼出单词所需的额外按键不会(不会)花费您任何费用......
  • @TimBrownlaw 感谢您的回答,先生,我该如何更改? .我不明白发生了什么……你能回答吗?
  • @devpro 您好,先生,感谢您的回答。我的控制台没有错误。我需要做 echo valid_errors(); ?我会把它放在哪里?里面是假的?
  • 在视图文件中添加这将告诉你实际的错误

标签: php ajax codeigniter


【解决方案1】:

您只需将确认密码验证的规则更改为:

array( 'field' => 'confPword', 'label' => 'confPword', 'rules' => 'trim|required|matches[curPword]' ),

您实际使用的是什么,您使用的是matches[password],但您的密码字段名称是matches[curPword]

【讨论】:

  • 非常感谢您,先生。我从您身上学到了一些东西。回显验证错误();
  • @jc-john 很高兴帮助你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 2021-01-19
相关资源
最近更新 更多