【发布时间】:2014-07-29 21:16:50
【问题描述】:
我在 Symfony 2.4 中验证用户密码时遇到问题。 我在 twig 中有一个使用 html 代码创建的表单,但我没有使用表单生成器,因为我通过 ajax 提交表单。
该表单是更改密码表单,我有一个密码字段,该字段必须与用户密码匹配。
代码:
表单的Html.twig代码:
<form id="changePassword" name="changePassword">
<label id="labelPassword">Write your current password </label>
<input type="password" id="CurrentPassword" name="CurrentPassword" />
<label id="labelNewPassword">Write your new password </label>
<input type="password" id="NewPassword" name ="NewPassword" />
<label id="labelNewPassword2">Repeat your new password</label>
<input type="password" id="NewPassword2" name ="NewPassword2" />
<input type="submit" class="btn-primary btn" value="Change"/>
</form>
ajax 代码:
var ServerData;
$(document).ready(function() {
$("form").submit(function(e) {
e.preventDefault();
var data = $(this).serialize();
var url = $(this).attr("name");
var id = $(this).attr("id");
if(validates(url)){
$.ajax({
url: url+"/" ,
method: "post",
dataType: "json",
data: data,
success: function (ServerData){
successFunction();
},
error: function (){
errorFunction();
}
});
}
else{
novalidFunction();
}
});
});
function validate(url){
//Just length and matching new password with repeat new password validations
}
// succesFunction(), errorFunction() and novalidFunction() and all this code are
//working great
控制器的php代码:
public function changePasswordAction ($request Request){
$user= $this->getUser();
$password = $user->getPassword();
$currentPassword = $request->get("CurrentPassword");
$newPassword = $request->get("NewPassword");
//here is where i need the code to compare $password with $currentPassword;
//the problem is that $password is encoded
//then i got the code to insert new values in Users table and its working;
}
在此先感谢,对我的英语感到抱歉
【问题讨论】:
标签: ajax forms validation passwords symfony-2.4