任何人都可以帮助我,为什么我的代码会返回if ($this->form_validation->run() == FALSE)
。我是codeigniter的初学者,我想使用form_validation来匹配更改密码的规则。我只想将语句form_validation返回true,以便我可以继续在数据库中进行更新。
我已经加载了form_validation,url等。 我的callback_oldpassword代码工作正常,我不需要在我的问题中覆盖它。所以永远不要回复它已经返回的回调。
我想要的是专注于我的密码匹配?我认为这是错误的做法。任何人都可以帮我找出会发生什么。 ?根据我声明的规则将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>";
}
}
答案 0 :(得分:3)
您只需将确认密码验证的规则更改为:
array( 'field' => 'confPword', 'label' => 'confPword', 'rules' => 'trim|required|matches[curPword]' ),
您实际使用的是matches[password]
,但密码字段名称为matches[curPword]